rm(list=ls()) 

# R version 4.3.1 (June 2023).

# load packages
library(GGIR) # version 3.0-1
library(GGIRread)
library(readxl)
library(dplyr)
library(utils)
library(fs)
library(stringr)
library(padr) # version 0.6.2
library(tibble)
library(timetk)
library(ggplot2)
library(plotly)
library(ActCR) # version 0.3.0
library(nparACT) # version 0.8
library(lubridate)
library(scales)
#------------------------------------------------------------------------------------------------------------------------------------
# Initial processing of GENEActiv raw data with GGIR: Raw Accelerometer Data Analysis 
# script input: a GENEActiv binary file (or a folder of GENEActiv binary files)
# script output: CSV files containing epoch values for each participant 

# GENEActiv wrist monitors collect raw gravitational acceleration values across three directional axes over time. 
# GGIR reads GENEActiv bin files, conducts automatic calibration, detects abnormal and non-wear time, and calculates the average magnitude of dynamic acceleration according to a number of user-specific metrics. 
# The summary measure of dynamic acceleration that GGIR produces is called Euclidean Norm Minus One (ENMO).
# Euclidean Norm consists of summing the squared acceleration for each of the accelerometer’s X-, Y-, and Z-axes. ENMO is then calculated at each time point, or epoch, by subtracting gravity (1 g, or 9.81 m/s2) from the Euclidean Norm. 
# In this project, GGIR processing produced data at a sampling rate of 1/5 Hz (5-second epochs), with one ENMO acceleration value recorded every 5 seconds. 
#------------------------------------------------------------------------------------------------------------------------------------

# n=66 participants .bin files (input for this script) are located on VR lab desktop computer (Yilei/Dan Albrecht's old computer) under a folder under my name (Joanna)
# GGIR processing for those n=66 participants was done on that computer due to it being time-intensive. It is recommended to run the GGIR script for large batches overnight. 

# 2 participants' (102-86-462 and 133-97-102) raw data were processed separately from the main batch and are located with Joanna

GGIR(
  mode=c(1:5),
  datadir="/Users/joanna/Pa_Lab/LearnIT_July2023/input", # change to input directory (folder containing binary files)
  outputdir="/Users/joanna/Pa_Lab/LearnIT_July2023/output", # change to desired output directory
  f0=1, f1=66, # change f1 if you want to run multiple subjects 
  #------------------------------------------------------------------------------------------------------------------------------------
  # Part 1: reading file, auto-calibration, and extracting features
  do.enmo = TRUE,             do.anglez=TRUE,
  chunksize=1,                printsummary=TRUE,
  idloc=2, #id is located in the filename
  #------------------------------------------------------------------------------------------------------------------------------------
  # Part 2: 
  desiredtz=c("America/Los_Angeles"),
  strategy = 2,               ndayswindow=7, #only the data between the first midnight and the last midnight is used for imputation.
  hrs.del.start = 0,          hrs.del.end = 0, #ndayswindow, hrs.del.start, hrs.del.end is not relevant 
  maxdur = 0,                 includedaycrit = 16,
  winhr = c(5,10), #window size in hours of L5 and M5 analysis
  qlevels = c(c(1380/1440),c(1410/1440)), #array of percentiles for which value needs to be extracted
  qwindow=c(0,24), #all variables will only be calculated over the full 24 hours in a day
  ilevels = c(seq(0,400,by=50),8000), #Levels for acceleration value frequency distribution in mg,
  mvpathreshold =c(100,120),
  epochvalues2csv=TRUE, #Epoch values are exported to CSV file. Here, non-wear time is imputed where possible. 
  mvpadur=c(5,10,15), #Three bout duration for which MVPA will be calculated
  bout.metric = 1,#Here, the algorithm looks for 10 minute windows in which more than XX percent of the epochs are above mvpathreshold,
  #and then counts the entire window as mvpa.
  closedbout=FALSE, #only count time spent above the threshold towards the bout duration.
  cosinor=TRUE, #performs cosinor analysis and extended analysis 
  #------------------------------------------------------------------------------------------------------------------------------------
  # Part 3: Sleep detection
  timethreshold= c(5),#Time threshold (minutes) for sustained inactivity periods detection    
  anglethreshold=5, #Angle threshold (degrees) for sustained inactivity periods detection
  ignorenonwear = TRUE, #ignore detected monitor non-wear periods to avoid confusion between monitor non-wear time and sustained inactivity
  #------------------------------------------------------------------------------------------------------------------------------------
  # Part 4: Integrating sleep log (if available) with sleep detection - storing day and person specific summaries of sleep
  excludefirstlast = TRUE, #the first and last night of the measurement are ignored for the sleep assessment.
  includenightcrit = 16, #Minimum number of valid hours per night
  def.noc.sleep = c(), #the 12 hour window centred at the least active 5 hours of
  #the 24 hour period will be used assume that  sustained inactivity  represents sleep.
  #loglocation= "C:/mydata/sleeplog.csv",
  outliers.only = F, #visualise only for nights with a difference in onset or waking time larger than the variable of argument criterror.
  #criterror = 4, # the number of minimum number of hours difference between sleep log and accelerometer estimate for the night to be included in the visualisation
  relyonsleeplog = FALSE,
  #sleeplogidnum = TRUE,
  #colid=1,
  #coln1=2,
  do.visual = TRUE,
  nnights = c(),
  #------------------------------------------------------------------------------------------------------------------------------------
  # Part 5: Merging physical activity with sleep analyses
  threshold.lig = c(30), #Threshold for light physical activity to separate inactivity from light.
  threshold.mod = c(100),#Threshold for moderate physical activity to separate light from moderate.
  threshold.vig = c(400),#Threshold for vigorous physical activity to separate moderate from vigorous.
  boutcriter = 0.8, #defines what fraction of a bout needs to be above the mvpathreshold
  boutcriter.in = 0.9, #defines what fraction of a bout needs to be below the light threshold
  boutcriter.lig = 0.8,#defines what fraction of a bout needs to be between the light and moderage threshold
  boutcriter.mvpa = 0.8, #fraction of a bout needs to be above the mvpathreshold
  boutdur.in = c(1,10,30),#Durations of inactivty bouts in minutes to be extracted.
  boutdur.lig = c(1,10),#Durations of light activty bouts in minutes to be extracted.
  boutdur.mvpa = c(1), #Durations of mvpa bouts in minutes to be extracted
  timewindow = c("WW"),#Timewindow over which summary statistics are derived (waking time to waking time)Runin
  #------------------------------------------------------------------------------------------------------------------------------------
  # Report generation: Generating reports based on meta-data
  do.report=c(2,3,4,5),
  visualreport=TRUE,     dofirstpage = TRUE,
  viewingwindow=1)
#------------------------------------------------------------------------------------------------------------------------------------
# participant 102-86-462 .bin (located with Joanna) was processed separately on Joanna's mac using the script above setting f1=1 and modifying directories appropriately
#------------------------------------------------------------------------------------------------------------------------------------
# participant 133-97-102 raw data was given as a large .csv (located with Joanna)
# GGIR code is meant for .bin but can be modified to process .csv using GGIR's read.myacc.csv
# script below modifies 133-97-102 .csv to be in the proper format for read.myacc.csv and then processes with GGIR 
#------------------------------------------------------------------------------------------------------------------------------------

# reading in participant 133-97-102
readin <- read.csv("/Users/joanna/Pa_Lab/LEARNit_Manuscript_Oct2024/133-97-102_csv/133-97-102.csv")

# prepping participant 133-97-102 for read.myacc.csv 
sub_133_97_102 <- readin %>% 
  slice(58:nrow(readin)) 
dateTime <- sub_133_97_102[seq(from = 1, to = nrow(sub_133_97_102),4),1]
acc_x <- sub_133_97_102[seq(from = 1, to = nrow(sub_133_97_102),4),2]
acc_y <- sub_133_97_102[seq(from = 2, to = nrow(sub_133_97_102),4),1]
acc_z <- sub_133_97_102[seq(from = 2, to = nrow(sub_133_97_102),4),2]
ambient_temp <- sub_133_97_102[seq(from = 4, to = nrow(sub_133_97_102),4),1]
sub_133_97_102_df <- data.frame(dateTime, acc_x, acc_y, acc_z, ambient_temp)
sub_133_97_102_df <-  sub_133_97_102_df %>%
  mutate(dateTime = as.POSIXct(dateTime, tz = "America/Los_Angeles"))
sub_133_97_102_df <- sub_133_97_102_df %>%
  mutate(acc_x = as.numeric(acc_x)) %>%
  mutate(acc_y = as.numeric(acc_y)) %>%
  mutate(acc_z = as.numeric(acc_z)) %>%
  mutate(ambient_temp = as.numeric(ambient_temp))

# exporting prepared data into .csv in order to use in read.myacc.csv
write.csv(sub_133_97_102_df, "/Users/joanna/Pa_Lab/LEARNit_Manuscript_Oct2024/133-97-102/133-97-102_prepared_for_GGIR.csv", row.names = FALSE)

# processing 133-97-102_prepared_for_GGIR.csv with GGIR's read.myacc.csv (will take a long time)

GGIR(
  mode=c(1:5),
  datadir="/Users/joanna/Pa_Lab/LEARNit_Manuscript_Oct2024/133-97-102/133-97-102_prepared_for_GGIR.csv",
  outputdir="/Users/joanna/Pa_Lab/LEARNit_Manuscript_Oct2024/133-97-102",
  studyname = "test", 
  f0=1, f1=1, # change f1 if you want to run multiple subjects at once
  #------------------------------------------------------------------------------------------------------------------------------------
  # Part 1: reading file, auto-calibration, and extracting features
  # read.myacc.csv arguments
  rmc.nrow = Inf,
  rmc.dec = ".",
  rmc.firstrow.acc = 1,
  rmc.col.acc = 2:4,
  rmc.col.time= 1,
  rmc.unit.acc = "g",
  rmc.unit.time = "POSIX",
  rmc.format.time = "%Y-%m-%d %H:%M:%OS",
  rmc.sf = 10.0, # 10 hz = 10 samples per second
  rmc.check4timegaps = FALSE,
  desiredtz = "America/Los_Angeles",
  configtz = "America/Los_Angeles",
  do.enmo = TRUE,             do.anglez=TRUE,
  chunksize=1,                printsummary=TRUE,
  idloc=2, #id is located in the filename
  #------------------------------------------------------------------------------------------------------------------------------------
  # Part 2: 
  strategy = 2,               ndayswindow=7, #only the data between the first midnight and the last midnight is used for imputation.
  hrs.del.start = 0,          hrs.del.end = 0, #ndayswindow, hrs.del.start, hrs.del.end is not relevant 
  maxdur = 0,                 includedaycrit = 16,
  winhr = c(5,10), #window size in hours of L5 and M5 analysis
  qlevels = c(c(1380/1440),c(1410/1440)), #array of percentiles for which value needs to be extracted
  qwindow=c(0,24), #all variables will only be calculated over the full 24 hours in a day
  ilevels = c(seq(0,400,by=50),8000), #Levels for acceleration value frequency distribution in mg,
  mvpathreshold =c(100,120),
  epochvalues2csv=TRUE, #Epoch values are exported to CSV file. Here, non-wear time is imputed where possible. 
  mvpadur=c(5,10,15), #Three bout duration for which MVPA will be calculated
  bout.metric = 1,#Here, the algorithm looks for 10 minute windows in which more than XX percent of the epochs are above mvpathreshold,
  #and then counts the entire window as mvpa.
  closedbout=FALSE, #only count time spent above the threshold towards the bout duration.
  cosinor=TRUE, #performs cosinor analysis and extended analysis 
  #------------------------------------------------------------------------------------------------------------------------------------
  # Part 3: Sleep detection
  timethreshold= c(5),#Time threshold (minutes) for sustained inactivity periods detection    
  anglethreshold=5, #Angle threshold (degrees) for sustained inactivity periods detection
  ignorenonwear = TRUE, #ignore detected monitor non-wear periods to avoid confusion between monitor non-wear time and sustained inactivity
  #------------------------------------------------------------------------------------------------------------------------------------
  # Part 4: Integrating sleep log (if available) with sleep detection - storing day and person specific summaries of sleep
  excludefirstlast = TRUE, #the first and last night of the measurement are ignored for the sleep assessment.
  includenightcrit = 16, #Minimum number of valid hours per night
  def.noc.sleep = c(), #the 12 hour window centred at the least active 5 hours of
  #the 24 hour period will be used assume that  sustained inactivity  represents sleep.
  #loglocation= "C:/mydata/sleeplog.csv",
  outliers.only = F, #visualise only for nights with a difference in onset or waking time larger than the variable of argument criterror.
  #criterror = 4, # the number of minimum number of hours difference between sleep log and accelerometer estimate for the night to be included in the visualisation
  relyonsleeplog = FALSE,
  #sleeplogidnum = TRUE,
  #colid=1,
  #coln1=2,
  do.visual = TRUE,
  nnights = c(),
  #------------------------------------------------------------------------------------------------------------------------------------
  # Part 5: Merging physical activity with sleep analyses
  threshold.lig = c(30), #Threshold for light physical activity to separate inactivity from light.
  threshold.mod = c(100),#Threshold for moderate physical activity to separate light from moderate.
  threshold.vig = c(400),#Threshold for vigorous physical activity to separate moderate from vigorous.
  boutcriter = 0.8, #defines what fraction of a bout needs to be above the mvpathreshold
  boutcriter.in = 0.9, #defines what fraction of a bout needs to be below the light threshold
  boutcriter.lig = 0.8,#defines what fraction of a bout needs to be between the light and moderage threshold
  boutcriter.mvpa = 0.8, #fraction of a bout needs to be above the mvpathreshold
  boutdur.in = c(1,10,30),#Durations of inactivty bouts in minutes to be extracted.
  boutdur.lig = c(1,10),#Durations of light activty bouts in minutes to be extracted.
  boutdur.mvpa = c(1), #Durations of mvpa bouts in minutes to be extracted
  timewindow = c("WW"),#Timewindow over which summary statistics are derived (waking time to waking time)Runin
  #------------------------------------------------------------------------------------------------------------------------------------
  # Report generation: Generating reports based on meta-data
  do.report=c(2,3,4,5),
  visualreport=TRUE,     dofirstpage = TRUE,
  viewingwindow=1)

#------------------------------------------------------------------------------------------------------------------------------------
# put all participants' GGIR-processed .csvs in a folder for further preparation 
#------------------------------------------------------------------------------------------------------------------------------------
# read in files from the folder of .csvs (each containing epoch data for each participant)
#------------------------------------------------------------------------------------------------------------------------------------

# assigning all csv learnIT files 
file_paths <- dir_ls("/Users/joanna/Pa_Lab/LEARNit_Manuscript_Oct2024/GGIR_processed_participant_csvs") # change directory to folder containing .csv files for each participant 

# writing for loop that reads all csvs into file_contents
file_contents <- list()
for (i in seq_along(file_paths)) {
  file_contents[[i]] <- read.csv(
    file = file_paths[[i]]
  )
}

# giving each file a name that is specified by its path
file_contents <- set_names(file_contents, file_paths)

# renaming file_contents to participant_id 
list_names <- names(file_contents)
list_names <- gsub('.bin.RData.csv', '', list_names)
list_names <- gsub('/Users/joanna/Pa_Lab/LEARNit_Manuscript_Oct2024/GGIR_processed_participant_csvs/', 'participant_', list_names)
names(file_contents) <- list_names

#------------------------------------------------------------------------------------------------------------------------------------
# Daylight savings time (DST) changes occur in March and November and introduces changes to circadian rhythms
# participants with collection spanning DST change must be identified 
#------------------------------------------------------------------------------------------------------------------------------------

# DST can be checked manually for each participant by seeing if their time series passes over DST for their year or..  
# function_daylightsavings determines which participants pass over daylight savings time starting or ending 
# in this dataset, participants with both -0800 and -0700 in their timestamp indicate a time change 

function_daylightsavings <- function(data.frame) {
  df_name <- deparse(substitute(data.frame))
  output <-  if (any(str_detect(data.frame$timestamp, "-0800")) & any(str_detect(data.frame$timestamp, "-0700"))) {
    print("Passes over DST")
  } else {
    print("NA")
  }
  return(output)
}

# applying function_daylightsavings dataframes
learnIT_daylightsavings <- lapply(file_contents, FUN = function_daylightsavings)

# defining which participants are impacted by daylight savings. 
# This dataframe tells you the 16 participant Id's impacted by DST and their corresponding row number in file_contents
participants_affectedby_daylightsavings <- which(learnIT_daylightsavings == "Passes over DST")

#------------------------------------------------------------------------------------------------------------------------------------
# removing DST for each of the 16 impacted participants 
#------------------------------------------------------------------------------------------------------------------------------------
view(participants_affectedby_daylightsavings)

# creating new dataset which includes learnIT_DST with participants affected by DST updated/replaced 
learnIT_DST_removed <- file_contents 

# participant_102-27-246, [[1]]
# change [[x]] throughout each block to match number corresponding to participant in participants_affectedby_daylightsavings
range(file_contents[[1]]$timestamp) # use range to figure out which DST --> 2020-03-08
learnIT_DST_removed[[1]] <- file_contents[[1]][!grepl("2020-03-08", file_contents[[1]]$timestamp),]

# participant_103-51-969, [[5]]
range(file_contents[[5]]$timestamp) # use range to figure out which DST --> 2019-11-03
learnIT_DST_removed[[5]] <- file_contents[[5]][!grepl("2019-11-03", file_contents[[5]]$timestamp),]

# participant_110-52-013, [[12]]
range(file_contents[[12]]$timestamp) # use range to figure out which DST --> 2017-11-05
learnIT_DST_removed[[12]] <- file_contents[[12]][!grepl("2017-11-05", file_contents[[12]]$timestamp),]

# participant_114-67-818, [[15]]
range(file_contents[[15]]$timestamp) # use range to figure out which DST --> 2018-03-11
learnIT_DST_removed[[15]] <- file_contents[[15]][!grepl("2018-03-11", file_contents[[15]]$timestamp),]

# participant_115-78-673, [[16]]
range(file_contents[[16]]$timestamp) # use range to figure out which DST --> 2020-11-01
learnIT_DST_removed[[16]] <- file_contents[[16]][!grepl("2020-11-01", file_contents[[16]]$timestamp),]

# participant_132-45-715, [[27]]
range(file_contents[[27]]$timestamp) # use range to figure out which DST --> 2019-11-03
learnIT_DST_removed[[27]] <- file_contents[[27]][!grepl("2019-11-03", file_contents[[27]]$timestamp),]
# after removing 1 day of DST this participant's data no longer crosses DST. 

# participant_133-74-716, [[28]]
range(file_contents[[28]]$timestamp) # use range to figure out which DST --> 2019-11-03
learnIT_DST_removed[[28]] <- file_contents[[28]][!grepl("2019-11-03", file_contents[[28]]$timestamp),]

# participant_136-78-498, [[33]]
range(file_contents[[33]]$timestamp) # use range to figure out which DST --> 2021-03-14
learnIT_DST_removed[[33]] <- file_contents[[33]][!grepl("2021-03-14", file_contents[[33]]$timestamp),]

# participant_136-82-057, [[34]]
range(file_contents[[34]]$timestamp) # use range to figure out which DST --> 2018-11-04
learnIT_DST_removed[[34]] <- file_contents[[34]][!grepl("2018-11-04", file_contents[[34]]$timestamp),]

# participant_138-93-032, [[38]]
range(file_contents[[38]]$timestamp) # use range to figure out which DST --> 2021-11-07
learnIT_DST_removed[[38]] <- file_contents[[38]][!grepl("2021-11-07", file_contents[[38]]$timestamp),]

#participant_143-21-413, [[42]]
range(file_contents[[42]]$timestamp) # use range to figure out which DST --> 2018-03-11
learnIT_DST_removed[[42]] <- file_contents[[42]][!grepl("2018-03-11", file_contents[[42]]$timestamp),]

# participant_144-24-565, [[43]]
range(file_contents[[43]]$timestamp) # use range to figure out which DST --> 2017-11-05
learnIT_DST_removed[[43]] <- file_contents[[43]][!grepl("2017-11-05", file_contents[[43]]$timestamp),]

# participant_148-32-931, [[48]]
range(file_contents[[48]]$timestamp) # use range to figure out which DST --> 2020-03-08
learnIT_DST_removed[[48]] <- file_contents[[48]][!grepl("2020-03-08", file_contents[[48]]$timestamp),]

# participant_151-59-472, [[52]]
range(file_contents[[52]]$timestamp) # use range to figure out which DST --> 2016-11-06
learnIT_DST_removed[[52]] <- file_contents[[52]][!grepl("2016-11-06", file_contents[[52]]$timestamp),]
# participant's end of collection is close to DST (1 day overlap)

# participant_155_41_441, [[59]]
range(file_contents[[59]]$timestamp) # use range to figure out which DST --> 2020-11-01
learnIT_DST_removed[[59]] <- file_contents[[59]][!grepl("2020-11-01", file_contents[[59]]$timestamp),]

# participant_165-42-096, [[66]]
range(file_contents[[66]]$timestamp) # use range to figure out which DST --> 2018-11-04
learnIT_DST_removed[[66]] <- file_contents[[66]][!grepl("2018-11-04", file_contents[[66]]$timestamp),]
#------------------------------------------------------------------------------------------------------------------------------------
# prepping and thickening the data  
# The ActCR’s ActExtendCosinor function requires that the time series data are divisible by 1440 (60-second epochs over 24 hours). 
# As such, accelerometer data must be downsampled using padr’s thicken function into average minute-level data, with one ENMO recording every minute (sampling rate of 1/60 Hz). 
#------------------------------------------------------------------------------------------------------------------------------------
function_prep <- function(data.frame) {
  output <- data.frame %>%
    dplyr::select(-anglez) %>% # removing anglez column 
    rename("time" = "timestamp") %>% # renaming timestamp column
    mutate(time = gsub('T', ' ',time)) %>% # removing T unncessary information from time 
    mutate(time = gsub("-[^-]+$", "", time)) %>% #removing -0800 or -0700 unnecessary information from time 
    mutate(time = as.POSIXct(time, tz = "America/Los_Angeles")) %>% # setting time to POSIXct date time format 
    thicken('min') %>% # padr's thicken downsamples to 1 sample per minute 
    group_by(time_min) %>%
    summarise(ENMO = mean(ENMO), .groups = 'drop') %>% # taking the mean ENMO for each minute 
  return(output)
}

# applying function_prep to every data frame in learnIT_DST_removed and assigning it to learnIT_prep
learnIT_prep <- lapply(learnIT_DST_removed, FUN = function_prep)

#------------------------------------------------------------------------------------------------------------------------------------
# slicing the data
# ActCR's ActExtendCosinor requires an input vector of ENMO acceleration values with no corresponding date-time values.
# To correctly compute acrotime it is thus necessary to ensure that the first ENMO acceleration value of the vector corresponds to time 00:00:00 (midnight) on the first full day of collection.
# To address this requirement, downsampled accelerometer data must be sliced to start at 00:00:00 (midnight) on the first full day of collection and end at 23:59:00 on the last full day of collection, ensuring that data are divisible by 1440 and corresponded to military time. 
#------------------------------------------------------------------------------------------------------------------------------------
# function_slice filters the data to start each participant at time 00:00 (midnight) and then slicing to divisible by 1440 epochs
function_slice <- function(data.frame) {
  midnight_row_number_plus_one <- which(str_detect(data.frame$time_min, "00:01:00")) # for some reason str_detect could not detect midnight 00:00:00
  midnight_row_number_plus_one_first <- midnight_row_number_plus_one[1]
  midnight_row_number <- midnight_row_number_plus_one_first - 1
  df_midnight <- data.frame %>%
    slice(midnight_row_number:nrow(data.frame))
  output <- df_midnight %>%
    slice(1:((nrow(df_midnight) %/% 1440)*1440)) # removing anything beyond 1440 epochs
  output <- output %>%
    as.data.frame()
  return(output)
}

# applying function_slice to every data frame from  list and assigning it to learnIT_prep
# check: each data frame should start at 00:00:00 and number of rows should be divisible by 1440
learnIT_sliced <- lapply(learnIT_prep, FUN = function_slice)

#------------------------------------------------------------------------------------------------------------------------------------
# after slicing data, pad() should be used to fill the gaps in incomplete datetime variables, by figuring out what the interval of the data is and what instances are missing.
#------------------------------------------------------------------------------------------------------------------------------------
function_pad <- function(data.frame) {
  output <- data.frame %>%
    pad() # pad will fill the gaps in incomplete datetime variables, by figuring out what the interval of the data is and what instances are missing.
  # checking to see if pad introduced any NAs that must be addressed manually 
  if (any(is.na(output))) {
    print("There are gaps in the data")
  } else {
    print("Data is fine")
  }
  return(output)
}

# applying function_pad to learnIT_sliced_DST
learnIT_sliced_padded <- lapply(learnIT_sliced, FUN = function_pad)

# the indices of participants with data missing from learnIT_sliced are: 1, 5, 12, 15, 16, 28, 33, 34, 38, 42, 43, 48, 52, 59, 66
# each of these participant's data were further investigated. 
# for all above participants, the incomplete data is due to daylight savings time and can be ignored so learnIT_sliced will be used forward with analysis 
#------------------------------------------------------------------------------------------------------------------------------------
# To address any circadian changes that time shifts may introduce, we decided to remove a full week (instead of just 1 day) of accelerometry data for each impacted participant starting on the day of their DST time change
#------------------------------------------------------------------------------------------------------------------------------------

# creating new dataset which includes learnIT_sliced with participants affected by DST updated/replaced 
learnIT_sliced_DST <- learnIT_sliced 

view(participants_affectedby_daylightsavings)

# participant_102-27-246, [[1]]
# change [[x]] throughout each block to match number corresponding to participant in participants_affectedby_daylightsavings
range(learnIT_sliced[[1]]$time_min) # use range to figure out which DST 
participant_102_27_246_pt1 <- learnIT_sliced[[1]] %>%
  filter_by_time(time_min, .start_date = "start", .end_date = "2020-03-07 23:59:00") # change .end_date to match day before DST starts at 23:59:00 (look up DST for that month/year)
participant_102_27_246_pt2 <- learnIT_sliced[[1]] %>%
  filter_by_time(time_min, .start_date = "2020-03-15 00:00:00", .end_date = "end") # change .start_date to be 7 days after DST at 00:00:00 
participant_102_27_246_merge <- rbind(participant_102_27_246_pt1, participant_102_27_246_pt2)
participant_102_27_246_merge <- participant_102_27_246_merge %>%
  slice(1:((nrow(participant_102_27_246_merge) %/% 1440)*1440))
learnIT_sliced_DST[[1]] <- participant_102_27_246_merge

# participant_103-51-969, [[5]]
range(learnIT_sliced[[5]]$time_min) # use range to figure out which DST 
participant_103_51_969_7days_pt1 <- learnIT_sliced[[5]] %>%
  filter_by_time(time_min, .start_date = "start", .end_date = "2019-11-02 23:59:00") 
participant_103_51_969_7days_pt2 <- learnIT_sliced[[5]] %>%
  filter_by_time(time_min, .start_date = "2019-11-10 00:00:00", .end_date = "end") 
participant_103_51_969_7days_merge <- rbind(participant_103_51_969_7days_pt1, participant_103_51_969_7days_pt2)
participant_103_51_969_7days_merge <- participant_103_51_969_7days_merge %>%
  slice(1:((nrow(participant_103_51_969_7days_merge) %/% 1440)*1440))
learnIT_sliced_DST[[5]] <- participant_103_51_969_7days_merge

# participant_110-52-013, [[12]]
range(learnIT_sliced[[12]]$time_min) # use range to figure out which DST 
participant_110_52_013_7days_pt1 <- learnIT_sliced[[12]] %>%
  filter_by_time(time_min, .start_date = "start", .end_date = "2017-11-04 23:59:00") 
participant_110_52_013_7days_pt2 <- learnIT_sliced[[12]] %>%
  filter_by_time(time_min, .start_date = "2017-11-12 00:00:00", .end_date = "end") 
participant_110_52_013_7days_merge <- rbind(participant_110_52_013_7days_pt1, participant_110_52_013_7days_pt2)
participant_110_52_013_7days_merge <- participant_110_52_013_7days_merge %>%
  slice(1:((nrow(participant_110_52_013_7days_merge) %/% 1440)*1440))
learnIT_sliced_DST[[12]] <- participant_110_52_013_7days_merge

# participant_114-67-818, [[15]]
range(learnIT_sliced[[15]]$time_min) # use range to figure out which DST 
participant_114_67_818_7days_pt1 <- learnIT_sliced[[15]] %>%
  filter_by_time(time_min, .start_date = "start", .end_date = "2018-03-10 23:59:00") 
participant_114_67_818_7days_pt2 <- learnIT_sliced[[15]] %>%
  filter_by_time(time_min, .start_date = "2018-03-18 00:00:00", .end_date = "end") 
participant_114_67_818_7days_merge <- rbind(participant_114_67_818_7days_pt1, participant_114_67_818_7days_pt2)
participant_114_67_818_7days_merge <- participant_114_67_818_7days_merge %>%
  slice(1:((nrow(participant_114_67_818_7days_merge) %/% 1440)*1440))
learnIT_sliced_DST[[15]] <- participant_114_67_818_7days_merge

# participant_115-78-673, [[16]]
range(learnIT_sliced[[16]]$time_min) # use range to figure out which DST 
participant_115_78_673_7days_pt1 <- learnIT_sliced[[16]] %>%
  filter_by_time(time_min, .start_date = "start", .end_date = "2020-10-31 23:59:00") 
participant_115_78_673_7days_pt2 <- learnIT_sliced[[16]] %>%
  filter_by_time(time_min, .start_date = "2020-11-08 00:00:00", .end_date = "end") 
participant_115_78_673_7days_merge <- rbind(participant_115_78_673_7days_pt1, participant_115_78_673_7days_pt2)
participant_115_78_673_7days_merge <- participant_115_78_673_7days_merge %>%
  slice(1:((nrow(participant_115_78_673_7days_merge) %/% 1440)*1440))
learnIT_sliced_DST[[16]] <- participant_115_78_673_7days_merge

# participant_132-45-715, [[27]]
range(learnIT_sliced[[27]]$time_min) # use range to figure out which DST 
# DST November 2019 was 11/3. participant_132-45-715 already spliced to not include 11/3. No further changes necessary

# participant_133-74-716, [[28]]
range(learnIT_sliced[[28]]$time_min) # use range to figure out which DST 
participant_133_74_716_7days_pt1 <- learnIT_sliced[[28]] %>%
  filter_by_time(time_min, .start_date = "start", .end_date = "2019-11-02 23:59:00") 
participant_133_74_716_7days_pt2 <- learnIT_sliced[[28]] %>%
  filter_by_time(time_min, .start_date = "2019-11-10 00:00:0", .end_date = "end") 
participant_133_74_716_7days_merge <- rbind(participant_133_74_716_7days_pt1, participant_133_74_716_7days_pt2)
participant_133_74_716_7days_merge <- participant_133_74_716_7days_merge %>%
  slice(1:((nrow(participant_133_74_716_7days_merge) %/% 1440)*1440))
learnIT_sliced_DST[[28]] <- participant_133_74_716_7days_merge

# participant_136-78-498, [[33]]
range(learnIT_sliced[[33]]$time_min) # use range to figure out which DST 
participant_136_78_498_7days_pt1 <- learnIT_sliced[[33]] %>%
  filter_by_time(time_min, .start_date = "start", .end_date = "2021-03-13 23:59:00") 
participant_136_78_498_7days_pt2 <- learnIT_sliced[[33]] %>%
  filter_by_time(time_min, .start_date = "2021-03-21 00:00:00", .end_date = "end") 
participant_136_78_498_7days_merge <- rbind(participant_136_78_498_7days_pt1, participant_136_78_498_7days_pt2)
participant_136_78_498_7days_merge <- participant_136_78_498_7days_merge %>%
  slice(1:((nrow(participant_136_78_498_7days_merge) %/% 1440)*1440))
learnIT_sliced_DST[[33]] <- participant_136_78_498_7days_merge

# participant_136-82-057, [[34]]
range(learnIT_sliced[[34]]$time_min) # use range to figure out which DST 
participant_136_82_057_7days_pt1 <- learnIT_sliced[[34]] %>%
  filter_by_time(time_min, .start_date = "start", .end_date = "2018-11-03 23:59:00") 
participant_136_82_057_7days_pt2 <- learnIT_sliced[[34]] %>%
  filter_by_time(time_min, .start_date = "2018-11-11 00:00:00", .end_date = "end") 
participant_136_82_057_7days_merge <- rbind(participant_136_82_057_7days_pt1, participant_136_82_057_7days_pt2)
participant_136_82_057_7days_merge <- participant_136_82_057_7days_merge %>%
  slice(1:((nrow(participant_136_82_057_7days_merge) %/% 1440)*1440))
learnIT_sliced_DST[[34]] <- participant_136_82_057_7days_merge

# participant_138-93-032, [[38]]
range(learnIT_sliced[[38]]$time_min) # use range to figure out which DST 
learnIT_participant_138_93_032_7days_pt1 <- learnIT_sliced[[38]] %>%
  filter_by_time(time_min, .start_date = "start", .end_date = "2021-11-06 23:59:00") 
learnIT_participant_138_93_032_7days_pt2 <- learnIT_sliced[[38]] %>%
  filter_by_time(time_min, .start_date = "2021-11-14 00:00:00", .end_date = "end") 
learnIT_participant_138_93_032_7days_merge <- rbind(learnIT_participant_138_93_032_7days_pt1, learnIT_participant_138_93_032_7days_pt2)
learnIT_participant_138_93_032_7days_merge <- learnIT_participant_138_93_032_7days_merge %>%
  slice(1:((nrow(learnIT_participant_138_93_032_7days_merge) %/% 1440)*1440))
learnIT_sliced_DST[[38]] <- learnIT_participant_138_93_032_7days_merge

#participant_143-21-413, [[42]]
range(learnIT_sliced[[42]]$time_min) # use range to figure out which DST 
learnIT_participant_143_21_413_7days_pt1 <- learnIT_sliced[[42]] %>%
  filter_by_time(time_min, .start_date = "start", .end_date = "2018-03-10 23:59:00") 
learnIT_participant_143_21_413_7days_pt2 <- learnIT_sliced[[42]] %>%
  filter_by_time(time_min, .start_date = "2018-03-18 00:00:00", .end_date = "end") 
learnIT_participant_143_21_413_7days_merge <- rbind(learnIT_participant_143_21_413_7days_pt1, learnIT_participant_143_21_413_7days_pt2)
learnIT_participant_143_21_413_7days_merge <- learnIT_participant_143_21_413_7days_merge %>%
  slice(1:((nrow(learnIT_participant_143_21_413_7days_merge) %/% 1440)*1440))
learnIT_sliced_DST[[42]] <- learnIT_participant_143_21_413_7days_merge

# participant_144-24-565, [[43]]
range(learnIT_sliced[[43]]$time_min) # use range to figure out which DST 
learnIT_participant_144_24_565_7days_pt1 <- learnIT_sliced[[43]] %>%
  filter_by_time(time_min, .start_date = "start", .end_date = "2017-11-04 23:59:00") 
learnIT_participant_144_24_565_7days_pt2 <- learnIT_sliced[[43]] %>%
  filter_by_time(time_min, .start_date = "2017-11-12 00:00:00", .end_date = "end") 
learnIT_participant_144_24_565_7days_merge <- rbind(learnIT_participant_144_24_565_7days_pt1, learnIT_participant_144_24_565_7days_pt2)
learnIT_participant_144_24_565_7days_merge <- learnIT_participant_144_24_565_7days_merge %>%
  slice(1:((nrow(learnIT_participant_144_24_565_7days_merge) %/% 1440)*1440))
learnIT_sliced_DST[[43]] <- learnIT_participant_144_24_565_7days_merge

# participant_148-32-931, [[48]]
range(learnIT_sliced[[48]]$time_min) # use range to figure out which DST 
learnIT_participant_148_32_931_7days_pt1 <- learnIT_sliced[[48]] %>%
  filter_by_time(time_min, .start_date = "start", .end_date = "2020-03-07 23:59:00") 
learnIT_participant_148_32_931_7days_pt2 <- learnIT_sliced[[48]] %>%
  filter_by_time(time_min, .start_date = "2020-03-15 00:00:00", .end_date = "end") 
learnIT_participant_148_32_931_7days_merge <- rbind(learnIT_participant_148_32_931_7days_pt1, learnIT_participant_148_32_931_7days_pt2)
learnIT_participant_148_32_931_7days_merge <- learnIT_participant_148_32_931_7days_merge %>%
  slice(1:((nrow(learnIT_participant_148_32_931_7days_merge) %/% 1440)*1440))
learnIT_sliced_DST[[48]] <- learnIT_participant_148_32_931_7days_merge

# participant_151-59-472, [[52]]
range(learnIT_sliced[[52]]$time_min) # use range to figure out which DST 
learnIT_participant_151_59_472_7days_pt1 <- learnIT_sliced[[52]] %>%
  filter_by_time(time_min, .start_date = "start", .end_date = "2016-11-05 23:59:00") 
learnIT_participant_151_59_472_7days_pt2 <- learnIT_sliced[[52]] %>%
  filter_by_time(time_min, .start_date = "2016-11-13 00:00:00", .end_date = "end") 
learnIT_participant_151_59_472_7days_merge <- rbind(learnIT_participant_151_59_472_7days_pt1, learnIT_participant_151_59_472_7days_pt2)
learnIT_participant_151_59_472_7days_merge <- learnIT_participant_151_59_472_7days_merge %>%
  slice(1:((nrow(learnIT_participant_151_59_472_7days_merge) %/% 1440)*1440))
learnIT_sliced_DST[[52]] <- learnIT_participant_151_59_472_7days_merge

# participant_155_41_441, [[59]]
range(learnIT_sliced[[59]]$time_min) # use range to figure out which DST 
learnIT_participant_155_41_441_7days_pt1 <- learnIT_sliced[[59]] %>%
  filter_by_time(time_min, .start_date = "start", .end_date = "2020-10-31 23:59:00") 
learnIT_participant_155_41_441_7days_pt2 <- learnIT_sliced[[59]] %>%
  filter_by_time(time_min, .start_date = "2020-11-08 00:00:00", .end_date = "end") 
learnIT_participant_155_41_441_7days_merge <- rbind(learnIT_participant_155_41_441_7days_pt1, learnIT_participant_155_41_441_7days_pt2)
learnIT_participant_155_41_441_7days_merge <- learnIT_participant_155_41_441_7days_merge %>%
  slice(1:((nrow(learnIT_participant_155_41_441_7days_merge) %/% 1440)*1440))
learnIT_sliced_DST[[59]] <- learnIT_participant_155_41_441_7days_merge

# participant_165-42-096, [[66]]
range(learnIT_sliced[[66]]$time_min) # use range to figure out which DST 
learnIT_participant_165_42_096_7days_pt1 <- learnIT_sliced[[66]] %>%
  filter_by_time(time_min, .start_date = "start", .end_date = "2018-11-03 23:59:00") 
learnIT_participant_165_42_096_7days_pt2 <- learnIT_sliced[[66]] %>%
  filter_by_time(time_min, .start_date = "2018-11-11 00:00:00", .end_date = "end") 
learnIT_participant_165_42_096_7days_merge <- rbind(learnIT_participant_165_42_096_7days_pt1, learnIT_participant_165_42_096_7days_pt2)
learnIT_participant_165_42_096_7days_merge <- learnIT_participant_165_42_096_7days_merge %>%
  slice(1:((nrow(learnIT_participant_165_42_096_7days_merge) %/% 1440)*1440))
learnIT_sliced_DST[[66]] <- learnIT_participant_165_42_096_7days_merge
#------------------------------------------------------------------------------------------------------------------------------------
# assessing number of days of accelerometry for each participant 
#------------------------------------------------------------------------------------------------------------------------------------
function_ndays <- function(data.frame) {
  output <- nrow(data.frame) / 1440
  return(output)
}

learnIT_sliced_DST_ndays <- lapply(learnIT_sliced_DST, FUN = function_ndays)
#------------------------------------------------------------------------------------------------------------------------------------
# each participant’s time series data must be plotted and manually inspected for visual abnormalities. 
# common visual abnormality: some participants had extra days beyond their collection period that consisted of nearly perfect sinusoidal activity. 
  # This abnormality was likely due to participant non-wear time after their ~30 day collection period had ended that GGIR processing of raw data misinterpreted as part of the collection period and imputed. 
    # All participants’ data were checked for unnaturally sinusoidal data, and these days were removed. 
#------------------------------------------------------------------------------------------------------------------------------------

learnIT_sliced_DST_averaged_removed <- learnIT_sliced_DST

# 1 - 45 days 
participant_102_27_246_data <- data.frame(learnIT_sliced_DST[[1]])
participant_102_27_246_plot <- ggplot(participant_102_27_246_data, aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_102_27_246_plot)
# Gap caused by 7 days removed for DST. All good.  

# 2 - 27 days 
participant_102_42_213_data <- data.frame(learnIT_sliced_DST[[2]])
participant_102_42_213_plot <- ggplot(participant_102_42_213_data, aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_102_42_213_plot)
# All good.  

# 3 - 32 days
participant_102_68_099_data <- data.frame(learnIT_sliced_DST[[3]])
participant_102_68_099_plot <- ggplot(participant_102_68_099_data, aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_102_68_099_plot)
# Two instances of potentially imputed data within the collection period, but does not warrant change. All good. 

# 4 - 31 days
participant_102_86_462_data <- data.frame(learnIT_sliced_DST[[4]])
participant_102_86_462_plot <- ggplot(participant_102_86_462_data, aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_102_86_462_plot)
# One instance of potentially imputed data within the collection period, but does not warrant change. All good. 

# 5 - 34 days
participant_103_51_969_data <- data.frame(learnIT_sliced_DST[[5]])
participant_103_51_969_plot <- ggplot(participant_103_51_969_data, aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_103_51_969_plot)
# Gap caused by 7 days removed for DST. All good.  

# 6 - 43 days.
participant_103_98_151_data <- data.frame(learnIT_sliced_DST[[6]])
participant_103_98_151_plot <- ggplot(participant_103_98_151_data, aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_103_98_151_plot)
# There are 7 days of averaged/imputed data that must be removed 
range(participant_103_98_151_data$time_min)
participant_103_98_151_data_imputed_removed <- participant_103_98_151_data %>%
  filter_by_time(time_min, .start_date = "2019-05-29 00:00:00", .end_date = " 2019-07-03 023:59:59")
# 7 days of data removed, now 36 days. Data frame in learnIT_sliced_DST_averaged_removed replaced
learnIT_sliced_DST_averaged_removed[[6]] <- participant_103_98_151_data_imputed_removed
participant_103_98_151_imputed_removed_plot <- ggplot(learnIT_sliced_DST_averaged_removed[[6]], aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_103_98_151_imputed_removed_plot)

# 7 - 33 days
participant_106_26_245_data <- data.frame(learnIT_sliced_DST[[7]])
participant_106_26_245_plot <- ggplot(participant_106_26_245_data, aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_106_26_245_plot)
# There are 5 days of averaged/imputed data that must be removed 
range(participant_106_26_245_data$time_min)
participant_106_26_245_data_imputed_removed <- participant_106_26_245_data %>%
  filter_by_time(time_min, .start_date = "2018-05-03 00:00:00", .end_date = "2018-05-30 023:59:59")
learnIT_sliced_DST_averaged_removed[[7]] <- participant_106_26_245_data_imputed_removed
participant_106_26_245_imputed_removed_plot <- ggplot(learnIT_sliced_DST_averaged_removed[[7]], aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_106_26_245_imputed_removed_plot)
# 5 days of data removed, now 28 days. Data frame in learnIT_sliced_DST_averaged_removed replaced

# 8 - 39 days, 
participant_107_76_610_data <- data.frame(learnIT_sliced_DST[[8]])
participant_107_76_610_plot <- ggplot(participant_107_76_610_data, aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_107_76_610_plot)
participant_107_76_610_data_imputed_removed <- participant_107_76_610_data %>%
  filter_by_time(time_min, .start_date = "2019-04-12 00:00:00", .end_date = "2019-05-13 023:59:59")
learnIT_sliced_DST_averaged_removed[[8]] <- participant_107_76_610_data_imputed_removed
participant_107_76_610_imputed_removed_plot <- ggplot(learnIT_sliced_DST_averaged_removed[[8]], aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_107_76_610_imputed_removed_plot)
# 7 days of data removed, now 32 days. Data frame in learnIT_sliced_DST_averaged_removed replaced

# 9 - 32 days
participant_107_81_343_data <- data.frame(learnIT_sliced_DST[[9]])
participant_107_81_343_plot <- ggplot(participant_107_81_343_data, aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_107_81_343_plot)
# All good

# 10 - 24 days
participant_109_09_497_data <- data.frame(learnIT_sliced_DST[[10]])
participant_109_09_497_plot <- ggplot(participant_109_09_497_data, aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_109_09_497_plot)
# All good

# 11 - 33 days
participant_109_79_543_data <- data.frame(learnIT_sliced_DST[[11]])
participant_109_79_543_plot <- ggplot(participant_109_79_543_data, aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_109_79_543_plot)
# All good

# 12 - 41 days
participant_110_52_013_data <- data.frame(learnIT_sliced_DST[[12]])
participant_110_52_013_plot <- ggplot(participant_110_52_013_data, aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_110_52_013_plot)
participant_110_52_013_data_imputed_removed <- participant_110_52_013_data %>%
  filter_by_time(time_min, .start_date = "2017-11-01 00:00:00", .end_date = "2017-12-04 023:59:59")
learnIT_sliced_DST_averaged_removed[[12]] <- participant_110_52_013_data_imputed_removed
participant_110_52_013_imputed_removed_plot <- ggplot(learnIT_sliced_DST_averaged_removed[[12]], aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_110_52_013_imputed_removed_plot)

# Gap caused by 7 days removed for DST. 14 days of data removed, now 27 days. Data frame in learnIT_sliced_DST_averaged_removed replaced

# 13 - 34 days
participant_110_65_076_data <- data.frame(learnIT_sliced_DST[[13]])
participant_110_65_076_plot <- ggplot(participant_110_65_076_data, aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_110_65_076_plot)
# All good

# 14 - 35 days
participant_111_03_584_data <- data.frame(learnIT_sliced_DST[[14]])
participant_111_03_584_plot <- ggplot(participant_111_03_584_data, aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_111_03_584_plot)
participant_111_03_584_data_imputed_removed <- participant_111_03_584_data %>%
  filter_by_time(time_min, .start_date = "2018-08-01 00:00:00", .end_date = "2018-08-27 023:59:59")
learnIT_sliced_DST_averaged_removed[[14]] <- participant_111_03_584_data_imputed_removed
participant_111_03_584_imputed_removed_plot <- ggplot(learnIT_sliced_DST_averaged_removed[[14]], aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_111_03_584_imputed_removed_plot)
# 8 days of data removed, now 27 days. Data frame in learnIT_sliced_DST_averaged_removed replaced

# 15 - 21 days
participant_114_67_818_data <- data.frame(learnIT_sliced_DST[[15]])
participant_114_67_818_plot <- ggplot(participant_114_67_818_data, aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_114_67_818_plot)
# Gap caused by 7 days removed for DST. All good.  

# 16 - 24 days
participant_115_78_673_data <- data.frame(learnIT_sliced_DST[[16]])
participant_115_78_673_plot <- ggplot(participant_115_78_673_data, aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_115_78_673_plot)
# Gap caused by 7 days removed for DST. All good.  

# 17 - 33 days
participant_144_24_565_data <- data.frame(learnIT_sliced_DST[[17]])
participant_144_24_565_plot <- ggplot(participant_144_24_565_data, aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_144_24_565_plot)
participant_144_24_565_data_imputed_removed <- participant_144_24_565_data %>%
  filter_by_time(time_min, .start_date = "2019-08-23 00:00:00", .end_date = "2019-09-22 023:59:59")
learnIT_sliced_DST_averaged_removed[[17]] <- participant_144_24_565_data_imputed_removed
participant_144_24_565_imputed_removed_plot <- ggplot(learnIT_sliced_DST_averaged_removed[[17]], aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_144_24_565_imputed_removed_plot)
# 2 days of data removed, now 31 days. Data frame in learnIT_sliced_DST_averaged_removed replaced

# 18 - 38 days
participant_116_81_481_data <- data.frame(learnIT_sliced_DST[[18]])
participant_116_81_481_plot <- ggplot(participant_116_81_481_data, aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_116_81_481_plot)
participant_116_81_481_data_imputed_removed <- participant_116_81_481_data %>%
  filter_by_time(time_min, .start_date = "2019-05-11 00:00:00", .end_date = "2019-06-11 023:59:59")
learnIT_sliced_DST_averaged_removed[[18]] <- participant_116_81_481_data_imputed_removed
participant_116_81_481_imputed_removed_plot <- ggplot(learnIT_sliced_DST_averaged_removed[[18]], aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_116_81_481_imputed_removed_plot)
# 1-3 days of potentially imputed data within middle of the collection period, but does not warrant change. 
# 6 days of data removed, now 32 days. Data frame in learnIT_sliced_DST_averaged_removed replaced

# 19 - 34 days
participant_119_69_861_data <- data.frame(learnIT_sliced_DST[[19]])
participant_119_69_861_plot <- ggplot(participant_119_69_861_data, aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_119_69_861_plot)
participant_119_69_861_data_imputed_removed <- participant_119_69_861_data %>%
  filter_by_time(time_min, .start_date = "2019-04-17 00:00:00", .end_date = "2019-05-16 023:59:59")
learnIT_sliced_DST_averaged_removed[[19]] <- participant_119_69_861_data_imputed_removed
participant_119_69_861_imputed_removed_plot <- ggplot(learnIT_sliced_DST_averaged_removed[[19]], aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_119_69_861_imputed_removed_plot)
# 4 days of data removed, now 30 days. Data frame in learnIT_sliced_DST_averaged_removed replaced

# 20 - 33 days
participant_121_13_329_data <- data.frame(learnIT_sliced_DST[[20]])
participant_121_13_329_plot <- ggplot(participant_121_13_329_data, aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_121_13_329_plot)
# All good

# 21 - 36 days
participant_121_74_107_data <- data.frame(learnIT_sliced_DST[[21]])
participant_121_74_107_plot <- ggplot(participant_121_74_107_data, aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_121_74_107_plot)
participant_121_74_107_data_imputed_removed <- participant_121_74_107_data %>%
  filter_by_time(time_min, .start_date = "2019-06-26 00:00:00", .end_date = "2019-07-28 023:59:59")
learnIT_sliced_DST_averaged_removed[[21]] <- participant_121_74_107_data_imputed_removed
participant_121_74_107_imputed_removed_plot <- ggplot(learnIT_sliced_DST_averaged_removed[[21]], aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_121_74_107_imputed_removed_plot)
# 3 days of data removed, now 33 days. Data frame in learnIT_sliced_DST_averaged_removed replaced

# 22 - 32 days
participant_121_76_951_data <- data.frame(learnIT_sliced_DST[[22]])
participant_121_76_951_plot <- ggplot(participant_121_76_951_data, aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_121_76_951_plot)
# All good

# 23 - 32 days
participant_122_15_029_data <- data.frame(learnIT_sliced_DST[[23]])
participant_122_15_029_plot <- ggplot(participant_122_15_029_data, aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_122_15_029_plot)
# All good

# 24 - 32 days
participant_122_24_435_data <- data.frame(learnIT_sliced_DST[[24]])
participant_122_24_435_plot <- ggplot(participant_122_24_435_data, aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_122_24_435_plot)
participant_122_24_435_data_imputed_removed <- participant_122_24_435_data %>%
  filter_by_time(time_min, .start_date = "2019-07-13 00:00:00", .end_date = "2019-08-12 023:59:59")
learnIT_sliced_DST_averaged_removed[[24]] <- participant_122_24_435_data_imputed_removed
participant_122_24_435_imputed_removed_plot <- ggplot(learnIT_sliced_DST_averaged_removed[[24]], aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_122_24_435_imputed_removed_plot)
# 1 day of data removed, now 31 days. Data frame in learnIT_sliced_DST_averaged_removed replaced

# 25 - 33 days
participant_123_05_623_data <- data.frame(learnIT_sliced_DST[[25]])
participant_123_05_623_plot <- ggplot(participant_123_05_623_data, aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_123_05_623_plot)
# All good

# 26 - 33 days
participant_126_59_522_data <- data.frame(learnIT_sliced_DST[[26]])
participant_126_59_522_plot <- ggplot(participant_126_59_522_data, aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_126_59_522_plot)
# One instance of potentially imputed data within middle of the collection period, but does not warrant change. All good. 

# 27 - 52 days, removed averaged data down to 30 days 
participant_132_45_715_data <- data.frame(learnIT_sliced_DST[[27]])
participant_132_45_715_plot <- ggplot(participant_132_45_715_data, aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_132_45_715_plot)
participant_132_45_715_data_imputed_removed <- participant_132_45_715_data %>%
  filter_by_time(time_min, .start_date = "2019-09-12 00:00:00", .end_date = "2019-10-11 023:59:59")
learnIT_sliced_DST_averaged_removed[[27]] <- participant_132_45_715_data_imputed_removed
participant_132_45_715_imputed_removed_plot <- ggplot(learnIT_sliced_DST_averaged_removed[[27]], aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_132_45_715_imputed_removed_plot)
# 22 days of data removed, now 30 days. Data frame in learnIT_sliced_DST_averaged_removed replaced

# 28 - 40 days
participant_133_74_716_data <- data.frame(learnIT_sliced_DST[[28]])
participant_133_74_716_plot <- ggplot(participant_133_74_716_data, aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_133_74_716_plot)
participant_133_74_716_data_imputed_removed <- participant_133_74_716_data %>%
  filter_by_time(time_min, .start_date = "2019-09-26 00:00:00", .end_date = "2019-11-02 023:59:59")
learnIT_sliced_DST_averaged_removed[[28]] <- participant_133_74_716_data_imputed_removed
participant_133_74_716_imputed_removed_plot <- ggplot(learnIT_sliced_DST_averaged_removed[[28]], aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_133_74_716_imputed_removed_plot)
# Gap caused by 7 days removed for DST.
# 2 days of data removed, now 38 days. Data frame in learnIT_sliced_DST_averaged_removed replaced
# the ending was just the 7 day time skip followed by averaged data so all was removed

# 29 - 27 days
participant_133_97_102_data <- data.frame(learnIT_sliced_DST[[29]])
participant_133_97_102_plot <- ggplot(participant_133_97_102_data, aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_133_97_102_plot)
# All good

# 30 - 43 days
participant_136_02_938_data <- data.frame(learnIT_sliced_DST[[30]])
participant_136_02_938_plot <- ggplot(participant_136_02_938_data, aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_136_02_938_plot)
ggplotly(participant_133_97_102_plot)
# All good

# 31 - 39 days, all looks good
participant_136_19_640_data <- data.frame(learnIT_sliced_DST[[31]])
participant_136_19_640_plot <- ggplot(participant_136_19_640_data, aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_136_19_640_plot)
# All good

# 32 - 32 days
participant_136_56_074_data <- data.frame(learnIT_sliced_DST[[32]])
participant_136_56_074_plot <- ggplot(participant_136_56_074_data, aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_136_56_074_plot)
# All good

# 33 - 28 days 
participant_136_78_498_data <- data.frame(learnIT_sliced_DST[[33]])
participant_136_78_498_plot <- ggplot(participant_136_78_498_data, aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_136_78_498_plot)
participant_136_78_498_data_imputed_removed <- participant_136_78_498_data %>%
  filter_by_time(time_min, .start_date = "2021-03-11 00:00:00", .end_date = "2021-04-12 023:59:59")
learnIT_sliced_DST_averaged_removed[[33]] <- participant_136_78_498_data_imputed_removed
participant_136_78_498_imputed_removed_plot <- ggplot(learnIT_sliced_DST_averaged_removed[[33]], aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_136_78_498_imputed_removed_plot)
# Gap caused by 7 days removed for DST
# 2 days of data removed, now 26 days. Data frame in learnIT_sliced_DST_averaged_removed replaced

# 34 - 42 days
participant_136_82_057_data <- data.frame(learnIT_sliced_DST[[34]])
participant_136_82_057_plot <- ggplot(participant_136_82_057_data, aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_136_82_057_plot)
participant_136_82_057_data_imputed_removed <- participant_136_82_057_data %>%
  filter_by_time(time_min, .start_date = "2018-10-16 00:00:00", .end_date = "2018-11-16 023:59:59")
learnIT_sliced_DST_averaged_removed[[34]] <- participant_136_82_057_data_imputed_removed
participant_136_82_057_imputed_removed_plot <- ggplot(learnIT_sliced_DST_averaged_removed[[34]], aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_136_82_057_imputed_removed_plot)
# Gap caused by 7 days removed for DST. All good.  
# 17 days of data removed, now 25 days. Data frame in learnIT_sliced_DST_averaged_removed replaced

# 35 - 32 days
participant_136_96_406_data <- data.frame(learnIT_sliced_DST[[35]])
participant_136_96_406_plot <- ggplot(participant_136_96_406_data, aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_136_96_406_plot)
participant_136_96_406_data_imputed_removed <- participant_136_96_406_data %>%
  filter_by_time(time_min, .start_date = "2018-05-24 00:00:00", .end_date = "2018-06-22 023:59:59")
learnIT_sliced_DST_averaged_removed[[35]] <- participant_136_96_406_data_imputed_removed
participant_136_96_406_imputed_removed_plot <- ggplot(learnIT_sliced_DST_averaged_removed[[35]], aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_136_96_406_imputed_removed_plot)
# 2 days of data removed, now 30 days. Data frame in learnIT_sliced_DST_averaged_removed replaced

# 36 - 34 days
participant_137_01_703_data <- data.frame(learnIT_sliced_DST[[36]])
participant_137_01_703_plot <- ggplot(participant_137_01_703_data, aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_137_01_703_plot)
participant_137_01_703_data_imputed_removed <- participant_137_01_703_data %>%
  filter_by_time(time_min, .start_date = "2018-07-06 00:00:00", .end_date = "2018-08-06  023:59:59")
learnIT_sliced_DST_averaged_removed[[36]] <- participant_137_01_703_data_imputed_removed
participant_137_01_703_imputed_removed_plot <- ggplot(learnIT_sliced_DST_averaged_removed[[36]], aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_137_01_703_imputed_removed_plot)
# 2 days of data removed, now 32 days. Data frame in learnIT_sliced_DST_averaged_removed replaced

# 37 - 30 days
participant_138_21_445_data <- data.frame(learnIT_sliced_DST[[37]])
participant_138_21_445_plot <- ggplot(participant_138_21_445_data, aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_138_21_445_plot)
# All good

# 38 - 28 days
participant_138_93_032_data <- data.frame(learnIT_sliced_DST[[38]])
participant_138_93_032_plot <- ggplot(participant_138_93_032_data, aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_138_93_032_plot)
# Gap caused by 7 days removed for DST. All good.  

# 39 - 41 days
participant_140_34_376_data <- data.frame(learnIT_sliced_DST[[39]])
participant_140_34_376_plot <- ggplot(participant_140_34_376_data, aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_140_34_376_plot)
participant_140_34_376_data_imputed_removed <- participant_140_34_376_data %>%
  filter_by_time(time_min, .start_date = "2021-09-15 00:00:00", .end_date = "2021-10-20 023:59:59")
learnIT_sliced_DST_averaged_removed[[39]] <- participant_140_34_376_data_imputed_removed
participant_140_34_376_data_imputed_removed_plot <- ggplot(learnIT_sliced_DST_averaged_removed[[39]], aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_140_34_376_data_imputed_removed_plot)
# 5 days of data removed, now 36 days. Data frame in learnIT_sliced_DST_averaged_removed replaced

# 40 - 33 days
participant_142_11_849_data <- data.frame(learnIT_sliced_DST[[40]])
participant_142_11_849_plot <- ggplot(participant_142_11_849_data, aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_142_11_849_plot)
participant_142_11_849_data_imputed_removed <- participant_142_11_849_data %>%
  filter_by_time(time_min, .start_date = "2019-05-04 00:00:00", .end_date = "2019-06-04 023:59:59")
learnIT_sliced_DST_averaged_removed[[40]] <- participant_142_11_849_data_imputed_removed
participant_142_11_849_imputed_removed_plot <- ggplot(learnIT_sliced_DST_averaged_removed[[40]], aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_142_11_849_imputed_removed_plot)
# 1 day of data removed, now 32 days. Data frame in learnIT_sliced_DST_averaged_removed replaced

# 41 - 39 days
participant_142_17_216_data <- data.frame(learnIT_sliced_DST[[41]])
participant_142_17_216_plot <- ggplot(participant_142_17_216_data, aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_142_17_216_plot)
# All good

# 42 - 44 days
participant_143_21_413_data <- data.frame(learnIT_sliced_DST[[42]])
participant_143_21_413_plot <- ggplot(participant_143_21_413_data, aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_143_21_413_plot)
participant_143_21_413_data_imputed_removed <- participant_143_21_413_data %>%
  filter_by_time(time_min, .start_date = "2018-03-07 00:00:00", .end_date = "2018-04-17 023:59:59")
learnIT_sliced_DST_averaged_removed[[42]] <- participant_143_21_413_data_imputed_removed
participant_143_21_413_imputed_removed_plot <- ggplot(learnIT_sliced_DST_averaged_removed[[42]], aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_143_21_413_imputed_removed_plot)
# 9 days of data removed, now 35 days. Data frame in learnIT_sliced_DST_averaged_removed replaced

# 43 - 43 days
participant_144_24_565_data <- data.frame(learnIT_sliced_DST[[43]])
participant_144_24_565_plot <- ggplot(participant_144_24_565_data, aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_144_24_565_plot)
participant_144_24_565_data_imputed_removed <- participant_144_24_565_data %>%
  filter_by_time(time_min, .start_date = "2017-09-23 00:00:00", .end_date = "2017-10-25 023:59:59")
learnIT_sliced_DST_averaged_removed[[43]] <- participant_144_24_565_data_imputed_removed
participant_144_24_565_imputed_removed_plot <- ggplot(learnIT_sliced_DST_averaged_removed[[43]], aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_144_24_565_imputed_removed_plot)
# 10 days of data removed, now 33 days. Data frame in learnIT_sliced_DST_averaged_removed replaced

# 44 - 28 days
participant_144_39_545_data <- data.frame(learnIT_sliced_DST[[44]])
participant_144_39_545_plot <- ggplot(participant_144_39_545_data, aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_144_39_545_plot)
# All good

# 45 - 33 days
participant_144_66_983_data <- data.frame(learnIT_sliced_DST[[45]])
participant_144_66_983_plot <- ggplot(participant_144_66_983_data, aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_144_66_983_plot)
# All good

# 46 - 31 days
participant_145_72_537_data <- data.frame(learnIT_sliced_DST[[46]])
participant_145_72_537_plot <- ggplot(participant_145_72_537_data, aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_145_72_537_plot)
# All good

# 47 - 33 days
participant_147_35_960_data <- data.frame(learnIT_sliced_DST[[47]])
participant_147_35_960_plot <- ggplot(participant_147_35_960_data, aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_147_35_960_plot)
# All good

# 48 - 41 days
participant_148_32_931_data <- data.frame(learnIT_sliced_DST[[48]])
participant_148_32_931_plot <- ggplot(participant_148_32_931_data, aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_148_32_931_plot)
participant_148_32_931_data_imputed_removed <- participant_148_32_931_data %>%
  filter_by_time(time_min, .start_date = "2020-02-28  00:00:00", .end_date = "2020-04-06 023:59:59")
learnIT_sliced_DST_averaged_removed[[48]] <- participant_148_32_931_data_imputed_removed
participant_148_32_931_imputed_removed_plot <- ggplot(learnIT_sliced_DST_averaged_removed[[48]], aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_148_32_931_imputed_removed_plot)
# Gap caused by 7 days removed for DST. 9 days of data removed, now 32 days. Data frame in learnIT_sliced_DST_averaged_removed replaced

# 49 - 42 days
participant_149_70_516_data <- data.frame(learnIT_sliced_DST[[49]])
participant_149_70_516_plot <- ggplot(participant_149_70_516_data, aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_149_70_516_plot)
participant_149_70_516_data_imputed_removed <- participant_149_70_516_data %>%
  filter_by_time(time_min, .start_date = "2021-09-02 00:00:00", .end_date = "2021-10-06 023:59:59")
learnIT_sliced_DST_averaged_removed[[49]] <- participant_149_70_516_data_imputed_removed
participant_149_70_516_imputed_removed_plot <- ggplot(learnIT_sliced_DST_averaged_removed[[49]], aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_149_70_516_imputed_removed_plot)
# 7 days of data removed, now 35 days. Data frame in learnIT_sliced_DST_averaged_removed replaced

# 50 - 43 days
participant_150_37_256_data <- data.frame(learnIT_sliced_DST[[50]])
participant_150_37_256_plot <- ggplot(participant_150_37_256_data, aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_150_37_256_plot)
participant_150_37_256_data_imputed_removed <- participant_150_37_256_data %>%
  filter_by_time(time_min, .start_date = "2018-12-11 00:00:00", .end_date = "2019-01-10 023:59:59")
learnIT_sliced_DST_averaged_removed[[50]] <- participant_150_37_256_data_imputed_removed
participant_150_37_256_imputed_removed_plot <- ggplot(learnIT_sliced_DST_averaged_removed[[50]], aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_150_37_256_imputed_removed_plot)
# 12 days of data removed, now 31 days. Data frame in learnIT_sliced_DST_averaged_removed replaced

# 51 - 49 days
participant_151_57_887_data <- data.frame(learnIT_sliced_DST[[51]])
participant_151_57_887_plot <- ggplot(participant_151_57_887_data, aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_151_57_887_plot)
participant_151_57_887_data_imputed_removed <- participant_151_57_887_data %>%
  filter_by_time(time_min, .start_date = "2019-04-03  00:00:00", .end_date = "2019-05-03 023:59:59")
learnIT_sliced_DST_averaged_removed[[51]] <- participant_151_57_887_data_imputed_removed
participant_151_57_887_imputed_removed_plot <- ggplot(learnIT_sliced_DST_averaged_removed[[51]], aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_151_57_887_imputed_removed_plot)
# 18 days of data removed, now 31 days. Data frame in learnIT_sliced_DST_averaged_removed replaced

# 52 - 30 days
participant_151_59_472_data <- data.frame(learnIT_sliced_DST[[52]])
participant_151_59_472_plot <- ggplot(participant_151_59_472_data, aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_151_59_472_plot)
# All good

# 53 - 30 days
participant_152_20_044_data <- data.frame(learnIT_sliced_DST[[53]])
participant_152_20_044_plot <- ggplot(participant_152_20_044_data, aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_152_20_044_plot)
# All good

# 54 - 31 days
participant_152_74_648_data <- data.frame(learnIT_sliced_DST[[54]])
participant_152_74_648_plot <- ggplot(participant_152_74_648_data, aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_152_74_648_plot)
# All good

# 55 - 32 days
participant_153_02_023_data <- data.frame(learnIT_sliced_DST[[55]])
participant_153_02_023_plot <- ggplot(participant_153_02_023_data, aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_153_02_023_plot)
participant_153_02_023_data_imputed_removed <- participant_153_02_023_data %>%
  filter_by_time(time_min, .start_date = "2017-06-17 00:00:00", .end_date = "2017-07-17 023:59:59")
learnIT_sliced_DST_averaged_removed[[55]] <- participant_153_02_023_data_imputed_removed
participant_153_02_023_imputed_removed_plot <- ggplot(learnIT_sliced_DST_averaged_removed[[55]], aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_153_02_023_imputed_removed_plot)
# 1 day of data removed, now 32 days. Data frame in learnIT_sliced_DST_averaged_removed replaced

# 56 - 35 days
participant_153_60_494_data <- data.frame(learnIT_sliced_DST[[56]])
participant_153_60_494_plot <- ggplot(participant_153_60_494_data, aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_153_60_494_plot)
# All good

# 57 - 38 days
participant_153_83_933_data <- data.frame(learnIT_sliced_DST[[57]])
participant_153_83_933_plot <- ggplot(participant_153_83_933_data, aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_153_83_933_plot)
# All good

# 58 - 31 days
participant_153_85_080_data <- data.frame(learnIT_sliced_DST[[58]])
participant_153_85_080_plot <- ggplot(participant_153_85_080_data, aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_153_85_080_plot)
# All good

# 59 - 44 days
participant_155_41_441_data <- data.frame(learnIT_sliced_DST[[59]])
participant_155_41_441_plot <- ggplot(participant_155_41_441_data, aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_155_41_441_plot)
participant_155_41_441_data_imputed_removed <- participant_155_41_441_data %>%
  filter_by_time(time_min, .start_date = "2020-10-30 00:00:00", .end_date = "2020-12-01 023:59:59")
learnIT_sliced_DST_averaged_removed[[59]] <- participant_155_41_441_data_imputed_removed
participant_155_41_441_imputed_removed_plot <- ggplot(learnIT_sliced_DST_averaged_removed[[59]], aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_155_41_441_imputed_removed_plot)
# Gap caused by 7 days removed for DST. 18 days of data removed, now 26 days. Data frame in learnIT_sliced_DST_averaged_removed replaced

# 60 - 48 days
participant_160_57_265_data <- data.frame(learnIT_sliced_DST[[60]])
participant_160_57_265_plot <- ggplot(participant_160_57_265_data, aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_160_57_265_plot)
participant_160_57_265_data_imputed_removed <- participant_160_57_265_data %>%
  filter_by_time(time_min, .start_date = "2018-12-06 00:00:00", .end_date = "2019-01-08 023:59:59")
learnIT_sliced_DST_averaged_removed[[60]] <- participant_160_57_265_data_imputed_removed
participant_160_57_265_imputed_removed_plot <- ggplot(learnIT_sliced_DST_averaged_removed[[60]], aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_160_57_265_imputed_removed_plot)
# 14 days of data removed, now 34 days. Data frame in learnIT_sliced_DST_averaged_removed replaced

# 61 - 33 days
participant_160_76_999_data <- data.frame(learnIT_sliced_DST[[61]])
participant_160_76_999_plot <- ggplot(participant_160_76_999_data, aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_160_76_999_plot)
# All good

# 62 - 30 days
participant_160_87_075_data <- data.frame(learnIT_sliced_DST[[62]])
participant_160_87_075_plot <- ggplot(participant_160_87_075_data, aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_160_87_075_plot)
# All good

# 63 - 30 days
participant_161_73_235_data <- data.frame(learnIT_sliced_DST[[63]])
participant_161_73_235_plot <- ggplot(participant_161_73_235_data, aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_161_73_235_plot)
participant_161_73_235_data_imputed_removed <- participant_161_73_235_data %>%
  filter_by_time(time_min, .start_date = "2018-02-07 00:00:00", .end_date = "2018-03-06 023:59:59")
learnIT_sliced_DST_averaged_removed[[63]] <- participant_161_73_235_data_imputed_removed
participant_161_73_235_imputed_removed_plot <- ggplot(learnIT_sliced_DST_averaged_removed[[63]], aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_161_73_235_imputed_removed_plot)
# 2 days of data removed, now 28 days. Data frame in learnIT_sliced_DST_averaged_removed replaced

# 64 - 39 days
participant_161_90_817_data <- data.frame(learnIT_sliced_DST[[64]])
participant_161_90_817_plot <- ggplot(participant_161_90_817_data, aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_161_90_817_plot)
# All good

# 65 - 41 days
participant_164_98_993_data <- data.frame(learnIT_sliced_DST[[65]])
participant_164_98_993_plot <- ggplot(participant_164_98_993_data, aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_164_98_993_plot)
participant_164_98_993_data_imputed_removed <- participant_164_98_993_data %>%
  filter_by_time(time_min, .start_date = "2019-03-26 00:00:00", .end_date = "2019-04-26 023:59:59")
learnIT_sliced_DST_averaged_removed[[65]] <- participant_164_98_993_data_imputed_removed
participant_164_98_993_imputed_removed_plot <- ggplot(learnIT_sliced_DST_averaged_removed[[65]], aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_164_98_993_imputed_removed_plot)
# 9 days of data removed, now 32 days. Data frame in learnIT_sliced_DST_averaged_removed replaced

# 66 - 33 days
participant_165_42_096_data <- data.frame(learnIT_sliced_DST[[66]])
participant_165_42_096_plot <- ggplot(participant_165_42_096_data, aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_165_42_096_plot)
participant_165_42_096_data_imputed_removed <- participant_165_42_096_data %>%
  filter_by_time(time_min, .start_date = "2018-10-02 00:00:00", .end_date = "2018-11-01 023:59:59")
learnIT_sliced_DST_averaged_removed[[66]] <- participant_165_42_096_data_imputed_removed
participant_165_42_096_imputed_removed_plot <- ggplot(learnIT_sliced_DST_averaged_removed[[66]], aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_165_42_096_imputed_removed_plot)
# 2 days of data removed, now 31 days. Data frame in learnIT_sliced_DST_averaged_removed replaced

# 67 - 33 days
participant_166_70_119_data <- data.frame(learnIT_sliced_DST[[67]])
participant_166_70_119_plot <- ggplot(participant_166_70_119_data, aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_166_70_119_plot)
# All good


# 68 - 51 days
participant_167_15_390_data <- data.frame(learnIT_sliced_DST[[68]])
participant_167_15_390_plot <- ggplot(participant_167_15_390_data, aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_167_15_390_plot)
participant_167_15_390_data_imputed_removed <- participant_167_15_390_data %>%
  filter_by_time(time_min, .start_date = "2018-11-16 00:00:00", .end_date = "2018-12-18 023:59:59")
learnIT_sliced_DST_averaged_removed[[68]] <- participant_167_15_390_data_imputed_removed
participant_167_15_390_imputed_removed_plot <- ggplot(learnIT_sliced_DST_averaged_removed[[68]], aes(x=time_min, y=ENMO)) + 
  geom_line() +
  labs(x = "Time",
       y = "Euclidean Norm Minus One (Gravitational Units)")
ggplotly(participant_167_15_390_imputed_removed_plot)
# 18 days of data removed, now 33 days. Data frame in learnIT_sliced_DST_averaged_removed replaced

#------------------------------------------------------------------------------------------------------------------------------------
# assessing number of days and sunmmary statistics of accelerometry for each participant after removing averaged data 
#------------------------------------------------------------------------------------------------------------------------------------
learnIT_sliced_DST_averaged_removed_ndays <- lapply(learnIT_sliced_DST_averaged_removed, FUN = function_ndays)
learnIT_sliced_DST_averaged_removed_ndays_numbers <- as.numeric(learnIT_sliced_DST_averaged_removed_ndays)
mean(learnIT_sliced_DST_averaged_removed_ndays_numbers)
sd(learnIT_sliced_DST_averaged_removed_ndays_numbers)
range(learnIT_sliced_DST_averaged_removed_ndays_numbers)

#------------------------------------------------------------------------------------------------------------------------------------
# actCR (Extended Cosinor Analysis)
# ActExtendCosinor function is used to extract acrotime for the 68 LEARNit participants using “window = 1” (each epoch was in a one-minute window), 
#------------------------------------------------------------------------------------------------------------------------------------

# writing a function for Extended Cosinor Analysis 
function_ActCR <- function(data.frame) {
  
  output_vector <- pull(data.frame, ENMO)
  output <- ActExtendCosinor(output_vector, window = 1)
  
  return(output)
}

learnIT_ActCR <- lapply(learnIT_sliced_DST_averaged_removed, FUN = function_ActCR)

# outputting ActCR results to data frame for learnIT
learnIT_ActCR_output <- as.data.frame(matrix(nrow = length(learnIT_ActCR), ncol = 10))
rownames(learnIT_ActCR_output) <- list_names
colnames(learnIT_ActCR_output) <- c('minimum', 'amp', 'alpha', 'beta', 'acrotime', 'F_pseudo', 'UpMesor', 'DownMesor', 'MESOR', 'ndays')

for (i in 1:length(learnIT_ActCR)) {
  learnIT_ActCR_output[i,1:10] <- data.frame((learnIT_ActCR[[i]]$params))
}

#------------------------------------------------------------------------------------------------------------------------------------
# nparACT
# nparACT’s nparACT_base function requires a data frame where the first column is a datetime vector and the second column is the corresponding ENMO acceleration value. 
# As such, it is not necessary to slice data. However, to remain consistent, the same input data for ActCR were used in nparACT after converting to the proper data format. 
# nparACT_base function was used to calculate IV using “SR = 1/60” (sampling rate in Hz = 1 sample every 60 seconds). 
# this takes a while 
#------------------------------------------------------------------------------------------------------------------------------------

# create an empty dataframe to fill with values with ncol = 7 for the 7 outputs of nparACT
learnIT_nparACT_output <- as.data.frame(matrix(nrow = length(list_names), ncol = 7))
rownames(learnIT_nparACT_output) <- list_names 
nparACT_labs <- c('IS', 'IV', 'RA', 'L5', 'L5_starttime', 'M10', 'M10_starttime')
colnames(learnIT_nparACT_output) <- nparACT_labs

# loop nparACT through the files 
for (i in 1:length(list_names)) {
  data_learnIT <- data.frame(learnIT_sliced_DST_averaged_removed[[i]])
  learnIT_nparACT_output[i,1:7] <- nparACT_base("data_learnIT", SR = 1/60, cutoff = 1, plot = FALSE, fulldays = FALSE)
}

# fixing the formatting of L5_starttime and M10_starttime
learnIT_nparACT_output <- learnIT_nparACT_output %>%
  mutate(L5_starttime = strtoi(as.difftime(L5_starttime, format = "%H:%M:%S", units = "mins")), L5_starttime) %>%
  mutate(L5_starttime = L5_starttime/60, L5_starttime) %>%
  mutate(L5_starttime = case_when(
    L5_starttime < 6 ~ L5_starttime + 24,
    L5_starttime > 6 ~ L5_starttime
  ), L5_starttime) %>%
  mutate(M10_starttime = strtoi(as.difftime(M10_starttime, format = "%H:%M:%S", units = "mins")), M10_starttime) %>%
  mutate(M10_starttime = M10_starttime/60, M10_starttime) 

#------------------------------------------------------------------------------------------------------------------------------------
# combining CR datasets and extracting summary statistics 
#------------------------------------------------------------------------------------------------------------------------------------
CR <- cbind(learnIT_ActCR_output, learnIT_nparACT_output)
CR <- CR %>%
  mutate(mri_id = row.names(CR), .before = minimum) %>%
  mutate(ndays = learnIT_sliced_DST_averaged_removed_ndays_numbers) # updating number of days to number where averaged days are removed 
row.names(CR) <- NULL

mean(CR$acrotime)
sd(CR$acrotime)

mean(CR$IV)
sd(CR$IV)

#------------------------------------------------------------------------------------------------------------------------------------
# creating plots to demonstrate low and high IV over 6 days 
#------------------------------------------------------------------------------------------------------------------------------------

# slicing the participant's data to 6 days and recalculating acrotime and IV, then plotting

# low IV - participant_111_03_584

participant_111_03_584_data_6days_forplot <- learnIT_sliced_DST_averaged_removed[[14]] %>%
  filter_by_time(time_min, .start_date = "2018-08-19 00:00:00", .end_date = "2018-08-24 23:59:00")
participant_111_03_584_data_6days_forplot_nparact <- nparACT_base("participant_111_03_584_data_6days_forplot", SR = 1/60, cutoff = 1, plot = FALSE, fulldays = FALSE)
# IV = 0.52
participant_111_03_584_data_6days_forplot_vector <- pull(participant_111_03_584_data_6days_forplot, ENMO)
participant_111_03_584_data_6days_forplot_vector_actcr <- ActExtendCosinor(participant_111_03_584_data_6days_forplot_vector, window = 1)
# acrotime = 12.28031

participant_111_03_584_data_1hour <- learnIT_sliced_DST_averaged_removed[[14]] %>%
  thicken(interval = 'hour', colname = "time_hour") %>%
  group_by(time_hour) %>%
  summarise(ENMO = mean(ENMO), .groups = 'drop')

participant_111_03_584_timeseries_1hour <- ggplot(participant_111_03_584_data_1hour, aes(x=time_hour, y=ENMO)) + 
  geom_line() +
  labs(x = "Time (Hours)",
       y = "Euclidean Norm Minus One (Gravitational Units)") + theme_apa() + scale_x_datetime(limits = c(as.POSIXct("2018-08-19 00:00:00"), as.POSIXct("2018-08-25 00:00:00")),expand = c(0,0), date_breaks="12 hours", date_labels = "%H:%M") +
  theme(axis.text.x=element_text(color = "black", size = "12", angle=60, hjust=1.1), axis.text.y=element_text(size = "12", color = "black")) + theme(axis.title.x=element_text(vjust = -0.5)) + theme(text = element_text(size=25)) + theme(axis.title.x = element_text(size = 20)) + theme(axis.title.y = element_text(size = 20)) + ylim(0, 0.13)
participant_111_03_584_timeseries_1hour

# high IV - participant_107_76_610

participant_107_76_610_data_6days_forplot <- learnIT_sliced_DST_averaged_removed[[8]] %>%
  filter_by_time(time_min, .start_date = "2019-04-24 00:00:00", .end_date = "2019-04-29 23:59:00")
participant_107_76_610_data_6days_forplot_nparact <- nparACT_base("participant_107_76_610_data_6days_forplot", SR = 1/60, cutoff = 1, plot = FALSE, fulldays = FALSE)
# IV = 1.16
participant_107_76_610_data_6days_forplot_vector <- pull(participant_107_76_610_data_6days_forplot, ENMO)
participant_107_76_610_data_6days_forplot_vector_actcr <- ActExtendCosinor(participant_107_76_610_data_6days_forplot_vector, window = 1)
# acrotime = 13.61506

participant_107_76_610_data_1hour <- learnIT_sliced_DST_averaged_removed[[8]] %>%
  thicken(interval = 'hour', colname = "time_hour") %>%
  group_by(time_hour) %>%
  summarise(ENMO = mean(ENMO), .groups = 'drop')

participant_107_76_610_timeseries_1hour <- ggplot(participant_107_76_610_data_1hour, aes(x=time_hour, y=ENMO)) + 
  geom_line() +
  labs(x = "Time (Hours)",
       y = "Euclidean Norm Minus One (Gravitational Units)") + theme_apa() + scale_x_datetime(limits = c(as.POSIXct("2019-04-24 00:00:00"), as.POSIXct("2019-04-30 00:00:00")),expand = c(0,0), date_breaks="12 hours", date_labels = "%H:%M") +
  theme(axis.text.x=element_text(color = "black", size = "12", angle=60, hjust=1.1), axis.text.y=element_text(size = "12", color = "black")) + theme(axis.title.x=element_text(vjust = -0.5)) + theme(text = element_text(size=25)) + theme(axis.title.x = element_text(size = 20)) + theme(axis.title.y = element_text(size = 20)) + ylim(0, 0.13) #+ theme(panel.grid.major.x = element_line(color = "grey", size = 0.5, linetype = 2)) 
participant_107_76_610_timeseries_1hour

#------------------------------------------------------------------------------------------------------------------------------------
# creating sleep dataset
#------------------------------------------------------------------------------------------------------------------------------------

sleep_full <- read.csv("/Users/joanna/Pa_Lab/LearnIT_Manuscript_Oct2024/sleep/part4_nightsummary_sleep_cleaned.csv")
sleep_full <- sleep_full %>%
  dplyr::select(ID, SptDuration, SleepDurationInSpt, WASO) %>%
  rename(mri_id = ID) %>%
  mutate(mri_id = gsub('\\.bin','',mri_id)) %>%
  mutate(mri_id = sub('1', 'participant_1', mri_id)) %>%
  mutate(sleep_efficiency = SleepDurationInSpt/SptDuration) %>%
  group_by(mri_id) %>%
  summarise(sleep_efficiency_mn=mean(sleep_efficiency), WASO_mn = mean(WASO), sleep_duration_mn = mean(SleepDurationInSpt)) %>%
  as.data.frame()

# 133_97_102 (GGIR-processed separately) 
sleep_133_97_102 <- read.csv("/Users/joanna/Pa_Lab/LearnIT_Manuscript_Oct2024/sleep/133_97_102_part4_nightsummary_sleep_cleaned.csv")
sleep_133_97_102 <- sleep_133_97_102 %>%
  dplyr::select(ID, SptDuration, SleepDurationInSpt, WASO) %>%
  rename(mri_id = ID) %>%
  mutate(mri_id = "participant_133-97-102") %>%
  mutate(sleep_efficiency = SleepDurationInSpt/SptDuration) %>%
  group_by(mri_id) %>%
  summarise(sleep_efficiency_mn=mean(sleep_efficiency), WASO_mn = mean(WASO), sleep_duration_mn = mean(SleepDurationInSpt)) %>%
  as.data.frame()

# 102_86_462 (GGIR-processed separately) 
sleep_102_86_462 <- read.csv("/Users/joanna/Pa_Lab/LearnIT_Manuscript_Oct2024/sleep/102_86_462_part4_nightsummary_sleep_cleaned.csv")
sleep_102_86_462 <- sleep_102_86_462 %>%
  dplyr::select(ID, SptDuration, SleepDurationInSpt, WASO) %>%
  rename(mri_id = ID) %>%
  mutate(mri_id = "participant_102-86-462") %>%
  mutate(sleep_efficiency = SleepDurationInSpt/SptDuration) %>%
  group_by(mri_id) %>%
  summarise(sleep_efficiency_mn=mean(sleep_efficiency), WASO_mn = mean(WASO), sleep_duration_mn = mean(SleepDurationInSpt)) %>%
  as.data.frame()

# combining sleep datasets
sleep <- rbind(sleep_full,sleep_133_97_102,sleep_102_86_462) %>%
  arrange(mri_id) %>%
  rename(SE = sleep_efficiency_mn) %>%
  rename(WASO = WASO_mn) %>%
  rename(SD = sleep_duration_mn) 

#------------------------------------------------------------------------------------------------------------------------------------
# creating AB dataset
#------------------------------------------------------------------------------------------------------------------------------------

AB <- read.table("/Users/joanna/Pa_Lab/LearnIT_Manuscript_Oct2024/AB/Extracted_SUVRs.txt")
colnames(AB) <- AB[1,]
AB <- AB[2:nrow(AB),]
rownames(AB) <- NULL
AB <- AB %>%
  rename(mri_id = Subject) %>%
  mutate(mri_id = sub('1', 'participant_1', mri_id)) %>% 
  dplyr::select(-c('composite_v2','APCC_v2','frontal_v2','lat_parietal_v2','lat_temporal_v2')) 

# pulling in individual files
AB_file_paths <- dir(path = "/Users/joanna/Pa_Lab/LearnIT_Manuscript_Oct2024/AB/individual", pattern = "*.txt", full.names = TRUE)
AB_file_contents <- list()
for (i in seq_along(AB_file_paths)) {
  AB_file_contents[[i]] <- read.table(
    file = AB_file_paths[[i]]
  )
}

AB_file_names <- list.files(path = "/Users/joanna/Pa_Lab/LearnIT_Manuscript_Oct2024/AB/individual", pattern = "*.txt")
AB_file_names <- gsub('.txt', '', AB_file_names)
names(AB_file_contents) <- AB_file_names

# creating data frame for 102-68-099
participant_102_68_099 <- as.data.frame(matrix(nrow = 1, ncol = 6))
colnames(participant_102_68_099) <- names(AB)
participant_102_68_099[1,1] <- 'participant_102-68-099'
participant_102_68_099[1,2:6] <- AB_file_contents[1:5]

# creating data frame for 103-51-969
participant_103_51_969 <- as.data.frame(matrix(nrow = 1, ncol = 6))
colnames(participant_103_51_969) <- names(AB)
participant_103_51_969[1,1] <- 'participant_103-51-969'
participant_103_51_969[1,2:6] <- AB_file_contents[6:10]

# creating data frame for 109-09-497
participant_109_09_497 <- as.data.frame(matrix(nrow = 1, ncol = 6))
colnames(participant_109_09_497) <- names(AB)
participant_109_09_497[1,1] <- 'participant_109-09-497'
participant_109_09_497[1,2:6] <- AB_file_contents[11:15]

# creating data frame for 123-05-623
participant_123_05_623 <- as.data.frame(matrix(nrow = 1, ncol = 6))
colnames(participant_123_05_623) <- names(AB)
participant_123_05_623[1,1] <- 'participant_123-05-623'
participant_123_05_623[1,2:6] <- AB_file_contents[16:20]

# creating data frame for 133-74-716
participant_133_74_716 <- as.data.frame(matrix(nrow = 1, ncol = 6))
colnames(participant_133_74_716) <- names(AB)
participant_133_74_716[1,1] <- 'participant_133-74-716'
participant_133_74_716[1,2:6] <- AB_file_contents[21:25]

# creating data frame for 142-11-849
participant_142_11_849 <- as.data.frame(matrix(nrow = 1, ncol = 6))
colnames(participant_142_11_849) <- names(AB)
participant_142_11_849[1,1] <- 'participant_142-11-849'
participant_142_11_849[1,2:6] <- AB_file_contents[26:30]

# creating data frame for 143-21-413
participant_143_21_413 <- as.data.frame(matrix(nrow = 1, ncol = 6))
colnames(participant_143_21_413) <- names(AB)
participant_143_21_413[1,1] <- 'participant_143-21-413'
participant_143_21_413[1,2:6] <- AB_file_contents[31:35]

# creating data frame for 148-32-931
participant_148_32_931 <- as.data.frame(matrix(nrow = 1, ncol = 6))
colnames(participant_148_32_931) <- names(AB)
participant_148_32_931[1,1] <- 'participant_148-32-931'
participant_148_32_931[1,2:6] <- AB_file_contents[36:40]

# combining full and individual AB datasets
AB <- rbind(AB, participant_102_68_099,participant_103_51_969,participant_109_09_497,participant_123_05_623,participant_133_74_716,participant_142_11_849,participant_143_21_413,participant_148_32_931)
AB <- AB %>%
  arrange(mri_id)
AB <- AB %>%
  rename(AB_composite = composite_v1) %>%
  rename(AB_APCC = APCC_v1) %>%
  rename(AB_frontal = frontal_v1) %>%
  rename(AB_lat_parietal = lat_parietal_v1) %>%
  rename(AB_lat_temporal = lat_temporal_v1 ) %>%
  mutate(AB_composite = as.numeric(AB_composite), AB_composite) %>%
  mutate(AB_APCC = as.numeric(AB_APCC), AB_APCC) %>%
  mutate(AB_frontal = as.numeric(AB_frontal), AB_frontal) %>%
  mutate(AB_lat_parietal = as.numeric(AB_lat_parietal), AB_lat_parietal) %>%
  mutate(AB_lat_temporal = as.numeric(AB_lat_temporal), AB_lat_temporal) 

mean(AB$AB_composite)
sd(AB$AB_composite)

#------------------------------------------------------------------------------------------------------------------------------------
# creating tau dataset
#------------------------------------------------------------------------------------------------------------------------------------

tau <- read.table("/Users/joanna/Pa_Lab/LearnIT_Manuscript_Oct2024/tau/Extracted_TAU_SUVRs.txt")
colnames(tau) <- tau[1,]
tau <- tau[2:nrow(tau),]
rownames(tau) <- NULL
tau <- tau %>%
  rename(mri_id = Subject) %>%
  mutate(mri_id = sub('1', 'participant_1', mri_id)) %>% 
  dplyr::select(-c('Tau_Braak_composite_ROI_v2','Tau_Braak_ROI_I-II_v2','Tau_Braak_ROI_III-IV_v2','Tau_Braak_ROI_V-VI_v2')) %>%
  rename('Tau_Braak_composite_ROI' = 'Tau_Braak_composite_ROI_v1', 'Tau_Braak_ROI_ItoII' = 'Tau_Braak_ROI_I-II_v1', 'Tau_Braak_ROI_IIItoIV' = 'Tau_Braak_ROI_III-IV_v1', 'Tau_Braak_ROI_VtoVI' = 'Tau_Braak_ROI_V-VI_v1') %>%
  mutate(Tau_Braak_composite_ROI = as.numeric(Tau_Braak_composite_ROI), Tau_Braak_composite_ROI) %>%
  mutate(Tau_Braak_ROI_ItoII = as.numeric(Tau_Braak_ROI_ItoII), Tau_Braak_ROI_ItoII) %>%
  mutate(Tau_Braak_ROI_IIItoIV = as.numeric(Tau_Braak_ROI_IIItoIV), Tau_Braak_ROI_IIItoIV) %>%
  mutate(Tau_Braak_ROI_VtoVI = as.numeric(Tau_Braak_ROI_VtoVI), Tau_Braak_ROI_VtoVI) 

# removing participant_140-34-376 because new data has been collected / QC'd
tau <- tau%>%
  filter(!grepl('participant_140-34-376', mri_id))

# pulling in tau file paths and contents for 8 new individual tau data 
tau_file_paths <- dir_ls("/Users/joanna/Pa_Lab/LearnIT_Manuscript_Oct2024/tau/individual")
tau_file_contents <- list()
for (i in seq_along(tau_file_paths)) {
  tau_file_contents[[i]] <- read.table(file = tau_file_paths[[i]])
}

# creating data frame to store these new variables 
tau_individual <- as.data.frame(matrix(nrow = 32, ncol = 2))
colnames(tau_individual) <- c('name','value')
tau_individual[1:32,1] <- tau_file_paths

for (i in seq_along(tau_file_contents)) {
  tau_individual[[i,2]] <- as.numeric(tau_file_contents[[i]])
}

# creating data frame for 103-51-969
tau_participant_103_51_969 <- as.data.frame(matrix(nrow = 1, ncol = 5))
colnames(tau_participant_103_51_969) <- names(tau)
tau_participant_103_51_969[1,1] <- 'participant_103-51-969'
tau_participant_103_51_969[1,2:5] <- tau_individual[1:4,2]

# creating data frame for 109-09-497
tau_participant_109_09_497 <- as.data.frame(matrix(nrow = 1, ncol = 5))
colnames(tau_participant_109_09_497) <- names(tau)
tau_participant_109_09_497[1,1] <- 'participant_109-09-497'
tau_participant_109_09_497[1,2:5] <- tau_individual[5:8,2]

# creating data frame for 123-05-623
tau_participant_123_05_623 <- as.data.frame(matrix(nrow = 1, ncol = 5))
colnames(tau_participant_123_05_623) <- names(tau)
tau_participant_123_05_623[1,1] <- 'participant_123-05-623'
tau_participant_123_05_623[1,2:5] <- tau_individual[9:12,2]

# creating data frame for 133-74-716
tau_participant_133_74_716 <- as.data.frame(matrix(nrow = 1, ncol = 5))
colnames(tau_participant_133_74_716) <- names(tau)
tau_participant_133_74_716[1,1] <- 'participant_133-74-716'
tau_participant_133_74_716[1,2:5] <- tau_individual[13:16,2]

# creating data frame for 140-34-376
tau_participant_140_34_376 <- as.data.frame(matrix(nrow = 1, ncol = 5))
colnames(tau_participant_140_34_376) <- names(tau)
tau_participant_140_34_376[1,1] <- 'participant_140-34-376'
tau_participant_140_34_376[1,2:5] <- tau_individual[17:20,2]

# creating data frame for 142-11-849
tau_participant_142_11_849 <- as.data.frame(matrix(nrow = 1, ncol = 5))
colnames(tau_participant_142_11_849) <- names(tau)
tau_participant_142_11_849[1,1] <- 'participant_142-11-849'
tau_participant_142_11_849[1,2:5] <- tau_individual[21:24,2]

# creating data frame for 143-21-413
tau_participant_143_21_413 <- as.data.frame(matrix(nrow = 1, ncol = 5))
colnames(tau_participant_143_21_413) <- names(tau)
tau_participant_143_21_413[1,1] <- 'participant_143-21-413'
tau_participant_143_21_413[1,2:5] <- tau_individual[25:28,2]

# creating data frame for 148-32-931
tau_participant_148_32_931 <- as.data.frame(matrix(nrow = 1, ncol = 5))
colnames(tau_participant_148_32_931) <- names(tau)
tau_participant_148_32_931[1,1] <- 'participant_148-32-931'
tau_participant_148_32_931[1,2:5] <- tau_individual[29:32,2]

# combining full and individual tau datasets
tau <- rbind(tau, tau_participant_103_51_969,tau_participant_109_09_497,tau_participant_123_05_623,tau_participant_133_74_716,tau_participant_140_34_376,tau_participant_142_11_849,tau_participant_143_21_413,tau_participant_148_32_931) %>%
  arrange(mri_id) 

mean(tau$Tau_Braak_ROI_ItoII)
sd(tau$Tau_Braak_ROI_ItoII)

mean(tau$Tau_Braak_ROI_IIItoIV)
sd(tau$Tau_Braak_ROI_IIItoIV)

#------------------------------------------------------------------------------------------------------------------------------------
# creating cognition datasets 
#------------------------------------------------------------------------------------------------------------------------------------

cog <- read_excel("/Users/joanna/Pa_Lab/LearnIT_Manuscript_Oct2024/cognition/LEARNit_DATA_LABELS_2022_12_20_BASELINE_JOANNA_highlighted.xlsx")
cog <- cog %>%
  dplyr::select(MRI_ID, MoCA_Total_Score, Letter_Number_Sequencing_Total_Raw_Score, WAIS_Digit_Symbol_Coding_Score_120_seconds, Trial_1_Free_Recall_Total_Correct, Trial_1_Free_Recall_Raw_Score, Trial_2_Free_Recall_Total_Correct, Trial_2_Free_Recall_Raw_Score, Trial_3_Free_Recall_Total_Correct, Trial_3_Free_Recall_Raw_Score, Trial_4_Free_Recall_Total_Correct, Trial_4_Free_Recall_Raw_Score, Trial_5_Free_Recall_Total_Correct, Trial_5_Free_Recall_Raw_Score, Short_Delay_Free_Recall_Total_Correct,Short_Delay_Free_Recall_Total_Correct_2, Long_Delay_Free_Recall_Total_Correct, Long_Delay_Free_Recall_Total_Correct_2) %>%
  replace_with_na_all(condition = ~.x %in% "NA" ) %>%
  mutate_at(c('MoCA_Total_Score', 'Letter_Number_Sequencing_Total_Raw_Score', 'WAIS_Digit_Symbol_Coding_Score_120_seconds', 'Trial_1_Free_Recall_Total_Correct', 'Trial_1_Free_Recall_Raw_Score', 'Trial_2_Free_Recall_Total_Correct', 'Trial_2_Free_Recall_Raw_Score', 'Trial_3_Free_Recall_Total_Correct', 'Trial_3_Free_Recall_Raw_Score', 'Trial_4_Free_Recall_Total_Correct', 'Trial_4_Free_Recall_Raw_Score', 'Trial_5_Free_Recall_Total_Correct', 'Trial_5_Free_Recall_Raw_Score', 'Short_Delay_Free_Recall_Total_Correct', 'Short_Delay_Free_Recall_Total_Correct_2', 'Long_Delay_Free_Recall_Total_Correct', 'Long_Delay_Free_Recall_Total_Correct_2'), as.numeric) %>%
  mutate(Free_Recall_Total_pt1 = Trial_1_Free_Recall_Total_Correct + Trial_2_Free_Recall_Total_Correct + Trial_3_Free_Recall_Total_Correct + Trial_4_Free_Recall_Total_Correct + Trial_5_Free_Recall_Total_Correct) %>%
  mutate(Free_Recall_Total_pt2 = Trial_1_Free_Recall_Raw_Score + Trial_2_Free_Recall_Raw_Score + Trial_3_Free_Recall_Raw_Score + Trial_4_Free_Recall_Raw_Score + Trial_5_Free_Recall_Raw_Score) %>%
  dplyr::select(!c(Trial_1_Free_Recall_Total_Correct, Trial_2_Free_Recall_Total_Correct, Trial_3_Free_Recall_Total_Correct, Trial_4_Free_Recall_Total_Correct, Trial_5_Free_Recall_Total_Correct, Trial_1_Free_Recall_Raw_Score, Trial_2_Free_Recall_Raw_Score, Trial_3_Free_Recall_Raw_Score, Trial_4_Free_Recall_Raw_Score, Trial_5_Free_Recall_Raw_Score)) %>%
  mutate(FreeRecall_AllTrials = coalesce(Free_Recall_Total_pt1, Free_Recall_Total_pt2)) %>%
  mutate(Short_Delay_Free_Recall = coalesce(Short_Delay_Free_Recall_Total_Correct, Short_Delay_Free_Recall_Total_Correct_2)) %>%
  mutate(Long_Delay_Free_Recall = coalesce(Long_Delay_Free_Recall_Total_Correct, Long_Delay_Free_Recall_Total_Correct_2)) %>%
  dplyr::select(!c(Free_Recall_Total_pt1, Free_Recall_Total_pt2, Short_Delay_Free_Recall_Total_Correct, Short_Delay_Free_Recall_Total_Correct_2, Long_Delay_Free_Recall_Total_Correct, Long_Delay_Free_Recall_Total_Correct_2)) %>%
  rename(mri_id = MRI_ID, MOCA = MoCA_Total_Score, LNS = Letter_Number_Sequencing_Total_Raw_Score, DS = WAIS_Digit_Symbol_Coding_Score_120_seconds, FR = FreeRecall_AllTrials, SDFR = Short_Delay_Free_Recall, LDFR = Long_Delay_Free_Recall) %>%
  mutate(mri_id = sub('1', 'participant_1', mri_id)) %>%
  mutate_at(c('MOCA', 'LNS', 'DS', 'FR', 'SDFR', 'LDFR'), as.numeric) %>%
  arrange(mri_id) %>%
  filter(!is.na(mri_id)) %>%
  as.data.frame()
# LNS missing 1 observation 

mean(cog$MOCA)
sd(cog$MOCA)

mean(cog$SDFR)
sd(cog$SDFR)

mean(cog$DS)
sd(cog$DS)

cog_flanker <- read.csv("/Users/joanna/Pa_Lab/LearnIT_Manuscript_Oct2024/cognition/Flanker_baseline_MRIID.csv")
cog_flanker <- cog_flanker %>%
  rename(mri_id = MRI.ID, Mean_RT_con = Mean.newRTcon, Mean_RT_incon = Mean.newRTinc) %>%
  mutate(mri_id = sub('1', 'participant_1', mri_id)) %>% 
  mutate(Mean_RT_diff = Mean_RT_incon - Mean_RT_con, .after = Mean_RT_incon) %>%
  filter(!is.na(Session)) %>%
  filter(!grepl('2018-02-15', SessionDate)) %>% # participant_121-76-951 has two data points (2017-06-28 and 2018-02-15). Baseline is likely the earlier 2017 one so this excludes the 2018 entry for this participant 
  filter(Mean.conDisplay.ACC >= 0.9) %>% # excluding participants with less than 90% accuracy for mean correct congruent and less than 70% for mean correct incongruent (originally 90% for both but Judy and I decided to include 1 participant) 
  filter(Mean.incDisplay.ACC >= 0.7) %>%
  dplyr::select(-c(Session, SessionDate,Mean.conDisplay.ACC, Mean.incDisplay.ACC))

#------------------------------------------------------------------------------------------------------------------------------------
# creating age dataset 
#------------------------------------------------------------------------------------------------------------------------------------
age <- read.csv("/Users/joanna/Pa_Lab/LearnIT_Manuscript_Oct2024/covariates/LEARNit_age.csv")
age <- age %>%
  rename(mri_id = MRI.ID) %>%
  dplyr::select(mri_id, Age) %>%
  filter(!is.na(mri_id)) %>%
  arrange(mri_id) %>%
  mutate(mri_id = sub('1', 'participant_1', mri_id))

#------------------------------------------------------------------------------------------------------------------------------------
# creating APOE4 dataset 
#------------------------------------------------------------------------------------------------------------------------------------
APOE4 <- read.csv("/Users/joanna/Pa_Lab/LearnIT_Manuscript_Oct2024/covariates/learnit_apoe_final_12_23_22.csv")
# (0=noncarrier, 1=carrier)
APOE4 <- APOE4 %>%
  rename(mri_id = MRI.ID) %>%
  dplyr::select(mri_id, APOE4) %>%
  filter(!is.na(mri_id)) %>%
  arrange(mri_id) %>%
  mutate(mri_id = sub('1', 'participant_1', mri_id)) 

#------------------------------------------------------------------------------------------------------------------------------------
# creating sex dataset 
#------------------------------------------------------------------------------------------------------------------------------------

sex <- read.csv("/Users/joanna/Pa_Lab/LearnIT_Manuscript_Oct2024/covariates/LEARNit_DATA_LABELS_2022_12_22_SCREENING_JOANNA.csv")
# (0=male, 1=female)
sex <- sex %>%
  rename('sex' = 'X7..Gender') %>%
  rename('mri_id' = 'MRI.ID') %>%
  dplyr::select(mri_id, sex) %>%
  filter(!is.na(mri_id)) %>%
  arrange(mri_id) %>%
  mutate(mri_id = sub('1', 'participant_1', mri_id)) %>%
  mutate(sex = gsub('Female', '1',sex)) %>%
  mutate(sex = gsub('Male', '0',sex)) 

#------------------------------------------------------------------------------------------------------------------------------------
# create years of education dataset 
#------------------------------------------------------------------------------------------------------------------------------------
years_education <- read.csv("/Users/joanna/Pa_Lab/LearnIT_Manuscript_Oct2024/covariates/LEARNit_DATA_LABELS_2022_12_22_BASELINE_EDU_JOANNA.csv")
years_education <- years_education %>%
  rename(mri_id = MRI.ID) %>%
  rename(years = Years.of.regular.school) %>%
  dplyr::select(mri_id, years) %>%
  filter(!is.na(mri_id)) %>%
  arrange(mri_id) %>%
  mutate(mri_id = sub('1', 'participant_1', mri_id)) 
years_education[67,2] <- "16"
years_education <- years_education %>%
  mutate(years = as.numeric(years),years)

#------------------------------------------------------------------------------------------------------------------------------------
# covariates combined
#------------------------------------------------------------------------------------------------------------------------------------

covariates <- full_join(age, APOE4) %>%
  full_join(sex) %>%
  full_join(years_education)

#------------------------------------------------------------------------------------------------------------------------------------
# all datasets combined 
#------------------------------------------------------------------------------------------------------------------------------------

learnIT <- full_join(CR, sleep) %>%
  full_join(AB) %>%
  full_join(tau) %>%
  full_join(cog) %>%
  full_join(cog_flanker) %>%
  full_join(covariates) 

